在PUT Restful服务中使用JSON对象

时间:2022-01-01 19:34:44

I'm trying to implement a RESTful Service in Java that receives a JSON Object through a PUT request and automatically maps into a Java Object. I managed to do this in XML, but I can't do it using JSON. Here's what I want to do:

我试图在Java中实现一个RESTful服务,该服务通过PUT请求接收JSON对象并自动映射到Java对象。我用XML实现了这一点,但是我不能用JSON实现。以下是我想做的:

@PUT
@Consumes(MediaType.APPLICATION_XML)
public String putTodo(JAXBElement<Route> r) {
    Route route = r.getValue();
    route.toString();
    System.out.println("Received PUT XML Request");
    return "ok";
}

This works, but using JSON would be something similar, but I can't use JAXB, can I?

这是可行的,但是使用JSON也是类似的,但是我不能使用JAXB,对吗?

@PUT
@Consumes(MediaType.APPLICATION_JSON)
public String putTodo(<WHAT DO I PUT HERE>) {
    Route route = r.getValue();
    route.toString();
    System.out.println("Received PUT JSON Request");
    return "ok";
}

1 个解决方案

#1


6  

By default Jersey will use JAXB to process the JSON messages by leveraging the Jettison library.

在默认情况下,Jersey将使用JAXB来利用Jettison库处理JSON消息。

So you can do the following:

你可以这样做:

@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String putTodo(JAXBElement<Route> r) {
    Route route = r.getValue();
    route.toString();
    System.out.println("Received PUT XML/JSON Request");
    return "ok";
}

For More Information on Using Jettison with JAXB:

有关JAXB使用Jettison的更多信息:

#1


6  

By default Jersey will use JAXB to process the JSON messages by leveraging the Jettison library.

在默认情况下,Jersey将使用JAXB来利用Jettison库处理JSON消息。

So you can do the following:

你可以这样做:

@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String putTodo(JAXBElement<Route> r) {
    Route route = r.getValue();
    route.toString();
    System.out.println("Received PUT XML/JSON Request");
    return "ok";
}

For More Information on Using Jettison with JAXB:

有关JAXB使用Jettison的更多信息:

相关文章