简体   繁体   English

JAX-RS如何序列化嵌入在公共包装器对象中的JSON对象,并在不使用包装器的情况下反序列化

[英]JAX-RS How to serialise JSON objects that are embedded within a common wrapper object and deserialise without the wrapper

I intend to send JSON data to my JAX-RS endpoints like so: 我打算像这样将JSON数据发送到我的JAX-RS端点:

POST /myendpoint

{
  "field1": "something",
  "field2": "something else",
  "field3": 12345
}

Then when I retrieve an object I want it contained in a common wrapper: 然后,当我检索一个对象时,我希望它包含在一个通用包装中:

GET /myendpoint

{
  "type": "MyEndpoint"
  "items": [
    {
      "item":     {
        "id": 1,
        "field1": "something",
        "field2": "something else",
        "field3": 12345
      },
      "link": "https://api.site.com/myendpoint/1"
    },
    {
      "item":     {
        "id": 2,
        "field1": "different",
        "field2": "different else",
        "field3": 67890
      },
      "link": "https://api.site.com/myendpoint/2"
    }
  ],
  "page_size": 10,
  "page": 1,
  "total": 2,
  "message": ""
}

And

GET /myendpoint/2

{
  "type": "MyEndpoint"
  "items": [
    {
      "item":     {
        "id": 2,
        "field1": "different",
        "field2": "different else",
        "field3": 67890
      },
      "link": "https://api.site.com/myendpoint/2"
    }
  ],
  "page_size": 10,
  "page": 1,
  "total": 1,
  "message": ""
}

I am starting to use Jackson FasterXML in Jersey for automatic serialisation/deserialisation, ie: 我开始在Jersey中使用Jackson FasterXML进行自动序列化/反序列化,即:

@Provider
public class JsonObjectMapperProvider implements ContextResolver<ObjectMapper> {

    private final ObjectMapper objectMapper;

    public JsonObjectMapperProvider() {
        objectMapper = new ObjectMapper();
    }

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

}

And then in the resources: 然后在资源中:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void createMyEndpoint(MyEndpoint myEndpoint) {
    myEndpointDao.create(myEndpoint);
    // ...
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<MyEndpoint> createMyEndpoint() {
    // I'm not actually sure how to do this one yet!! but I include it for completeness
    return myEndpointDao.getAll();
}

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public MyEndpoint createMyEndpoint(@PathParam("{id}") id) {
    return myEndpointDao.read(id);
}

This would work for MyEndpoint objects that aren't contained in the wrapper but how would I include the wrapper? 这将适用于包装中未包含的MyEndpoint对象,但是如何包含包装? Or is there an altogether better way to do this? 还是有一种更好的方法呢?

The schema for the JSON isn't set in stone, if something else makes more sense then I'm all ears. JSON的模式并不是一成不变的,如果还有其他更有意义的地方,那我就不胜枚举了。

Here I write few suggestion I have in mind reading your question: 在这里,我想提出一些建议来阅读您的问题:

  1. return always something from your methods, even if you're handling a POST method. 即使您正在处理POST方法,也总是从您的方法返回某些内容。 This will make the caller aware of request status. 这将使呼叫者知道请求状态。
  2. instead of return your model objects, I think is better if your methods return always an object javax.ws.rs.core.Response 我认为,如果您的方法始终返回一个对象javax.ws.rs.core.Response ,而不是返回模型对象,则更好
  3. if you're worried about the serialization (I mean the use of the wrapper) just use the Response object to serialize your response. 如果您担心序列化(我的意思是使用包装器),只需使用Response对象来序列化您的响应。 For example for a success response use: Response.ok().entity(yourReturnObject).build() , this will care of the serialization part almost transparently (you don't need to handle the objectMapper). 例如,对于成功的响应使用: Response.ok().entity(yourReturnObject).build() ,这将几乎透明地处理序列化部分(您不需要处理objectMapper)。

This could be a trivial example of a method: 这可能是一个简单的方法示例:

@GET
@Path("/{id}")
@Produces({ "application/json" })
public Response createMyEndpoint(@PathParam("id") String id) {
    try {
        return Response.ok().entity(myEndpointDao.read(id)).build();
    } catch (Exception e) {
       Response.status(500).entity(e.getMessage()).build();
    }
}

I also suggest, to make you're life simpler, have a look at swagger . 我还建议,为使您的生活更简单,请大摇大摆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM