0

I want to send from my client-side application/json request with JSON

{ "content" : "My content", "question" : "WHY?" }

Is there some way in Jersey rest for retrieve this JSON on server side my some method like:

@POST
@Path("/myMethod")
@Consumes(MediaType.APPLICATION_JSON)
public void myServerMethod(@FormParam("content") String content, @FormParam("question") String question) {
         System.out.println(content+" "+question);
}

Now i can't retrieve values from json's body.

Best regards

makarovalv
  • 38
  • 6
  • 1
    Have a look at this question: http://stackoverflow.com/questions/1662490/consuming-json-object-in-jersey-service –  Feb 27 '14 at 02:14

1 Answers1

0

First, make an object representing your post data:

public class MyPostData
{
    private String content;
    private String question;

    // getters and setters here
}

Then, change your resource method to:

@POST
@Path("/myMethod")
@Consumes(MediaType.APPLICATION_JSON)
public void myServerMethod(MyPostData data)
{
    System.out.println(data.getContent() + " " + data.getQuestion());
}
Alden
  • 6,553
  • 2
  • 36
  • 50
  • Thanks. I know, that i can use class for retrieve application/json. But i don't want to create separate class for this purpose. – makarovalv Feb 28 '14 at 08:42
  • Ah ok. Are you using either Jackson, Moxy, or Jettison as a JSON library with Jersey? If so you can make the first method parameter a `JsonObject` (which will differ based on the json lib you are using) and pull the strings from that – Alden Feb 28 '14 at 14:42