获取JAX-RS服务以创建具有JsonObject属性的对象 [英] Getting JAX-RS service to create an object with a JsonObject property

查看:118
本文介绍了获取JAX-RS服务以创建具有JsonObject属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让JAXRS将特定类属性的所有json细节推送到JsonObject对象中。



假设我有以下类:

  public class MyMessage实现Serializable {

私有PayloadType payloadType;

私有JsonObject有效内容;
}

REST方法:

  @POST 
@Path(/ send)
@Consumes(MediaType.APPLICATION_JSON)
public response send(MyMessage message)

我想发布以下JSON,但是有有效负载属性设置为 javax.json.JsonObject 对象。

  {
payloadType:'MESSAGE',
payload:{
subject:My Subject,
body:这是一条消息
}

我在Glassfish上运行,所以我期待 JsonObject 包含在 org.glassfish.jersey.media 中,它支持包含在GF4.1中。添加下面的maven依赖只会导致不明确的类异常。

 < dependency> 
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-processing< / artifactId>
< version> 2.22.1< / version>
< /依赖关系>


解决方案

/ p>


  1. javax.json (或JSONP)不处理POJO。它只处理 javax.json API。如果您期望的是原始提供程序处理POJO,而JSONP提供程序处理 javax.json ,则它不能像那样工作。您可以使用处理POJO(不知道> javax.json >或使用处理> javax.json 。(我们确实做到了这一点: - )


  2. Glassfish的默认提供者是MOXy,所以我们需要禁用以使用任何其他提供者,要禁用MOXY,你需要设置这个属性


    ServerProperties.MOXY_JSON_FEATURE_DISABLE


    true 。无论是在您的web.xml还是您的Java配置中。


所以要做到这一点,我们应该确保我们使用的是杰克逊Jackson拥有 jackson-datatype-jsr353 模块,它可以完成你想要做的事情, javax。 json 在POJO中。



Glassfish已经拥有了Jackson提供者,但是您应该将其添加到 code>范围。因此,添加这两个依赖关系

 < dependency> 
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-jackson< / artifactId>
< version> 2.10.4< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> com.fasterxml.jackson.datatype< / groupId>
< artifactId> jackson-datatype-jsr353< / artifactId>
< version> 2.3.3< / version>
< /依赖关系>

如果yoo关注版本,我使用Glassfish 4.1中包含的相同版本。它使用泽西岛2.10.4和杰克逊2.3.3。你想要版本冲突。即使提供了 jersey-media-json-jackson ,编译时尝试使用与服务器相同的版本仍然是个不错的主意。



你也应该在你的应用程序中注册 JacksonFeature 我们需要的最后一件事是注册 JSR353Module ,以便我们可以通过Jackson获得 javax.json 支持。要做到这一点,只需在应用程序中注册以下提供程序即可。

  @Provider 
public class ObjectMapperProvider implements ContextResolver< ObjectMapper> {

private final ObjectMapper映射器;

public ObjectMapperProvider(){
mapper = new ObjectMapper();
mapper.registerModule(new JSR353Module());
}

@Override
public ObjectMapper getContext(Class<?> type){
return mapper;
}
}

就是这样。我测试过了,它应该可以工作。


I'd like to get JAXRS to push all json details for a specific class property into a JsonObject object.

Let's say I have the following class:

public class MyMessage implements Serializable {

    private PayloadType payloadType;    

    private JsonObject payload;
}

REST method of:

@POST
@Path("/send")
@Consumes(MediaType.APPLICATION_JSON)
public Response send(MyMessage message) 

I'd like to POST the following JSON, but have the payload property set as a javax.json.JsonObject object.

{
   payloadType:'MESSAGE',
   payload:{
      subject:"My Subject",
      body:"This is a message"
   }
}

I'm running on Glassfish, so I was expecting that message reader for JsonObject were included with org.glassfish.jersey.media, which is support to be included in the GF4.1. Add the following maven dependency just causes ambiguous class exceptions.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.22.1</version>
</dependency>

解决方案

So there are a couple things stopping you here.

  1. javax.json (or JSONP) does not handle POJOs. It only handles the javax.json API. If you what you are expecting is that the original provider handle the POJO, while the JSONP provider handle the javax.json, it doesn't work like that. Either you will use the one that handles the POJOs (which doesn't know javax.json or you use the one that handles javax.json. (We do make this happen below though :-)

  2. Glassfish's default provider is MOXy. So we need to disable to to use any other provider. To disable MOXy, you need to set this property

    ServerProperties.MOXY_JSON_FEATURE_DISABLE

    to true. Either in your web.xml or your Java config.

So to make this work, we should make sure that we are using Jackson. Jackson has the jackson-datatype-jsr353 module which allows to do exactly what you are trying to do, javax.json in POJO.

Glassfish has the Jackson provider already, but you should add it anyway in a provided scope. So add these two dependencies

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.10.4</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr353</artifactId>
    <version>2.3.3</version>
</dependency>

If yoo pay attention to the versions, I am using the same versions that are included in Glassfish 4.1. It uses Jersey 2.10.4 and Jackson 2.3.3. You want the versions to conflict. Even though the jersey-media-json-jackson is provided, it's still a good idea to try and use the same version as the server, when compiling.

Also you should register the JacksonFeature with your application.

And the last thing we need is to register the JSR353Module so that we can get the javax.json support with Jackson. To do that just register the following provider with your application.

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public ObjectMapperProvider() {
        mapper = new ObjectMapper();
        mapper.registerModule(new JSR353Module());
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }
}

That's it. I've tested it, and it should work.

这篇关于获取JAX-RS服务以创建具有JsonObject属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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