0

I'm trying to take JSON values on POST method but I don't know how to do this.

This is the code that I have:

@POST
@Path("/contacts")
@Consumes({"application/json"})
public void addContact() {

    HttpSession session = request.getSession(true);

    UserPK user = new UserPK((String) session.getAttribute("username"));
    //dest = TAKE JSON VALUE

    dao.addContact(user, dest);
}

I'm sending the JSON value with firefox RestClient with this look:

{"param1":"value"}

How can I take this value?

We also have this error message: 415 Unsupported Media Type

Thanks.

1
  • Can you explain a bit more? Is "dest" your JSON object or is it just the value from your JSON object that you extract? When does the error message occur? Commented Jan 10, 2016 at 17:31

3 Answers 3

2

Make sure you are actually sending a post and the content-type of your request is application/json. To consume this with spring MVC you can use the @RequestBody annotation:

@POST
@Path("/contacts")
@Consumes({"application/json"})
public void addContact(@RequestBody MyClass c) {

Where MyClass is a simple java class representing the request object (i.e. in this case you would just need one String class variable named param1.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using springmvc, it comes with message converter for json. You may have to declare your pojo as a parameter in your method.

Comments

0

The problem was that the plugin from firefox didn't allow me to modify the headers, so when I tryed with text/plain on chrome with postman it worked.

Thanks anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.