在resteasy中使用JSON.stringify和JSON.parse [英] Use JSON.stringify and JSON.parse in resteasy

查看:106
本文介绍了在resteasy中使用JSON.stringify和JSON.parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在包含resteasy服务的Java类中使用JSON.stringify和JSON.parse来与Jquery脚本交换信息. 哪个库应该导入Java类,或者我应该做什么?,因为在脚本本身中,默认情况下让我执行该操作.非常感谢.

I need to use JSON.stringify and JSON.parse in a java class that contains resteasy services to exchange information with a Jquery script. Which library should import the java class or what should I do ?, because in the script itself let me do it by default. Thank you very much.

推荐答案

因此,据我了解,您希望能够对Java对象进行JSON序列化和反序列化,因为JSON.stringifyJSON.parse对于Javascript所做的工作

So from my understanding you want to be able to serialize and deserialize JSON to and from Java object, as that's what JSON.stringify and JSON.parse does for Javascript.

要能够处理此问题,我们需要一个MessageBodyReader和一个MessageBodyWriter来处理转换. Resteasy的提供程序已作为框架的一部分提供.我们只需要添加模块即可.

To be able to handle that, we need a MessageBodyReader and a MessageBodyWriter to handle the conversion. The Resteasy has the providers available as part of the framework. We just need to add the module.

希望您正在使用Maven,如果没有的话请参阅这篇文章.使用Maven,您应该具有以下任一依赖项

Hopefully you are using Maven, if not see this post. With Maven you should have either one of these dependencies

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>

唯一的区别是将使用杰克逊风味1.x或2.x

The only difference is in which Jackson flavor will be used 1.x or 2.x

一旦您必须在类路径上提供程序,就应该对其进行自动配置.您需要做的就是使用您的POJO广告,例如,完成

Once you have to provider on the classpath, it should be auto configured. All you need to do is use your POJO ad the conversion will be done e.g

@GET
@Produces("application/json")
public Response getMyPojo() {
    MyPojo pojo = new MyPojo();
    return Response.ok(pojo).build();
}

@POST
@Consumes("application/json")
public Response createMyPojo( MyPojo pojo ) {
    // do something with pojo
}

这篇关于在resteasy中使用JSON.stringify和JSON.parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆