读取JSON以与JAX-RS进行映射 [英] Reading JSON to map with JAX-RS

查看:99
本文介绍了读取JSON以与JAX-RS进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java EE REST应用程序(它在Glassfish 4.1下运行),并且在读取简单的键值JSON列表时遇到问题。我试图使用POJO类生成和使用JSON,生产工作正常,但消耗不起作用。
让我们来看看代码。我用于生产和使用JSON的POJO类如下所示:

  public class Transport {
protected Integer id;
保护字符串名称;
保护地图< String,String>领域;
// ... getters and setters ...
}

REST资源导入这些库(我把这里放在相关的库中):

  import javax.ws.rs.Consumes; 
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

产生JSON的方法可以正常工作,在这里它是:

  @GET 
@Produces(MediaType.APPLICATION_JSON)
@Path(test)
public response testGet(){
Transport sct = new Transport();
sct.setId(1);
sct.setName(test);
HashMap< String,String> t1 = new HashMap();
t1.put(aaa,bbb);
t1.put(bbb,ccc);
t1.put(ccc,ddd);
sct.setFields(t1);
// createOkResponse是一个返回类型的类方法
return createOkResponse(sct);
}

所以我正确地获得这个输出:

  {
id:1,
name:test,
fields:{
aaa:bbb,
ccc:ddd,
bbb:ccc
}
}

使用相同POJO的方法使用JSON:

  @PUT 
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(test)
public response testPut Transport sct){
Map< String,String> fields = sct.getFields();
//此代码在fields.entrySet()
//因为字段总是空
// for(Entry< String,String> field:fields.entrySet())时会抛出NullPointerException {
// LOG.log(Level.INFO,*** field:{0} = {1},new Object [] {field.getKey(),field.getValue()});
//}
return createOkResponse(sct);

$ / code>

使用上面相同的JSON服务将导致一个带id的sct对象和名称属性设置,但字段属性为null,因此该方法的响应为:

  {
id :1,
name:test,
fields:null
}

它就像 {aaa:bbb,ccc:ddd,bbb:ccc} JSON对象被忽略。我尝试过使用Google搜索,但我不明白我的代码有什么问题,并且我没有在glassfish日志中看到错误或警告。
任何想法?感谢。

解决方案

您可以使用 XmlAdapter a href =https://stackoverflow.com/a/29324520/2587435>这篇文章。问题是MOXy处理地图的方式。它使用JAXB,这是为了用于XML,而XML不能很好地映射到Maps(或键/值)对。



这个原因,而不是使用MOXy for JSON(这是Glassfish中默认的JSON提供程序),我会推荐使用Jackson。杰克逊也应该被纳入Glassfish 4.1。但是你需要禁用MOXY,所以Jackson可以注册。要禁用MOXy,只需设置Jersey属性 jersey.config.server.disableMoxyJson true


I'm writing a Java EE REST application (it runs under Glassfish 4.1) and I'm having problems reading a simple key-value JSON list. I'm trying to produce and consume a JSON using a POJO class, producing works fine, but consuming is not working. Let's get to the code. The POJO class I use for produce and consume JSON is like this:

public class Transport {
  protected Integer id;
  protected String name;
  protected Map<String, String> fields;
  // ...getters and setters...
}

The REST resource import those libraries (I've put here the relevant ones):

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

The method who produces the JSON works fine, here it is:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("test")
public Response testGet() {
  Transport sct = new Transport();
  sct.setId(1);
  sct.setName("test");
  HashMap<String, String> t1 = new HashMap();
  t1.put("aaa", "bbb");
  t1.put("bbb", "ccc");
  t1.put("ccc", "ddd");
  sct.setFields(t1);
  // createOkResponse is a class method that return Response type
  return createOkResponse(sct);
}

so I correctly obtain this output:

{
  "id": 1,
  "name": "test",
  "fields": {
     "aaa": "bbb",
     "ccc": "ddd",
     "bbb": "ccc"
  }
}

The method who consume the JSON, using the same POJO:

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("test")
public Response testPut(Transport sct) {
  Map<String, String> fields = sct.getFields();
  // this code throws a NullPointerException at fields.entrySet() 
  // because fields is always null
  //for (Entry<String, String> field : fields.entrySet()) {
  //  LOG.log(Level.INFO, "*** field: {0} = {1}", new Object[]{field.getKey(), field.getValue()});
  //}
  return createOkResponse(sct);
}

Consuming the service with the same JSON above will result in an sct object with id and name properties set, but with fields property null, so the response of the method is:

{
  "id": 1,
  "name": "test",
  "fields": null
}

it's like the {"aaa": "bbb", "ccc": "ddd", "bbb": "ccc"} JSON object is being ignored. I've tried googling around but I don't understand what is wrong with my code, and I've seen no errors or warnings in the glassfish log. Any idea? Thanks.

解决方案

You can use an XmlAdapter, as mentioned in this post. The problem is the way that MOXy handles Maps. It uses JAXB under the hood, which was meant to be used for XML, and XML doesn't map very well to Maps (or key/value) pairs.

For this reason, instead of using MOXy for JSON (which is the default JSON provider in Glassfish), I would recommend using Jackson. Jackson should also be included in Glassfish 4.1. But you will need to disable MOXy, so the Jackson can register itself. To disable MOXy, just set the Jersey property jersey.config.server.disableMoxyJson to true.

这篇关于读取JSON以与JAX-RS进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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