Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume JSON Object in PUT Restful Service

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:

@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?

@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";
}
like image 934
ffleandro Avatar asked Apr 19 '11 16:04

ffleandro


1 Answers

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

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:

  • http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison.html
  • http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison-namespace.html
like image 154
bdoughan Avatar answered Sep 30 '22 03:09

bdoughan