How do I create a POST request in REST to accept JSON input?

I am trying to learn RESTful web services. And I am creating a simple set of web services. Stuck when I started working on POST.

I want to pass JSON input to POST method. This is what I did in code:

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/x-www-form-urlencoded", produces="text/plain")
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody String createChangeRequest(MyCls mycls) {
    return "YAHOOOO!!"; 
}

      

I have included Jackson in my POM.xml.

 <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-lgpl</artifactId>
    <version>1.9.13</version>
</dependency>   

      

MyCls is a simple class with a few getters and setters.

I am calling the above POST service from a simple REST client from chrome.

URL: http://localhost:8080/MYWS/cls/create
Data: {<valid-json which corresponds to each variable in the MyCls pojo}

      

I see the answer below:

415 Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

      

I tried adding the header as "application / json" in the POST request in the REST client, but it didn't help.

Can anyone let me know what I am missing here? How can I automatically map my input JSON to the MyCls mailbox? Am I missing any configuration here?

Edit: MyCls.java

public class MyCls{
   private String name;
   private String email;
   private String address;
       public String getName() {
    return name;
   }
   public void setName(String name) {
    name= name;
   }
       ---similar getter and setter for email, address--
}

      

json from chrome Simple REST client:

{"name":"abc", "email":"de@test","address":"my address"}

      

Edit: Changed my controller method to the following, but still see the same error:

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/json", produces="text/plain")
@ResponseStatus(HttpStatus.CREATED)
 public @ResponseBody String createChangeRequest(@RequestBody MyCls mycls) {
  return "YAHOOOO!!"; 
 }

      

+2


source to share


1 answer


Assuming your client is submitting application/json

as its content type, then the handler mapped to

consumes="application/x-www-form-urlencoded"

      

can't handle it. The actual Content-type

does not match the expected.

If you expect application/json

, you should instead

consumes="application/json"

      

Moreover, declaring



public @ResponseBody String createChangeRequest(MyCls mycls) {

      

(in default environment), equivalent to

public @ResponseBody String createChangeRequest(@ModelAttribute MyCls mycls) {

      

This means that the object MyCls

is created from the request parameters and not from the JSON body. Instead, you should have

public @ResponseBody String createChangeRequest(@RequestBody MyCls mycls) {

      

so that Spring will deserialize your JSON to an object of type MyCls

.

+4


source







All Articles