我打算将JSON数据发送到我的JAX-RS端点,如下所示:
POST /myendpoint
{
"field1": "something",
"field2": "something else",
"field3": 12345
}
然后当我检索一个对象时,我希望它包含在一个共同的包装器中:
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": ""
}
和
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": ""
}
我开始在泽西岛使用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;
}
}
然后在资源中:
@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);
}
这适用于未包含在包装器中的MyEndpoint对象,但是如何包含包装器?或者有更好的方法来做到这一点吗?
JSON的架构并不是一成不变的,如果其他东西更有意义,那么我就是全部耳朵。
答案 0 :(得分:1)
在这里,我写了一些建议,我在考虑你的问题:
javax.ws.rs.core.Response
Response
对象来序列化您的响应。例如,对于成功响应使用:Response.ok().entity(yourReturnObject).build()
,这将几乎透明地处理序列化部分(您不需要处理objectMapper)。 这可能是方法的一个简单例子:
@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();
}
}
我还建议,为了让您的生活更简单,请查看swagger。