psms
psms

Reputation: 883

Spring: JSON to Java Object using @RequestBody

I'am working with spring Rest Web service.I'am not able to convert JSON to Java Object using @RequestBody.

Controller Method:

@RequestMapping(value="/test",method=RequestMethod.POST)
public @ResponseBody String test(@RequestBody Student s)
{

    System.out.print(s.getName()+s.getMark()+s.getRollNo());

    return "ok";
}

POJO class:

 public class Student implements Serializable {


private static final long serialVersionUID = 1L;
private int mark;
private String name;
private int rollNo;
    // getters and setters
  }

MessageConverter in Serlvet-context.xml:

    <beans:bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></beans:bean>
 <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
  <beans:list>
    <beans:ref bean="jacksonMessageConverter"/>
  </beans:list>
</beans:property>
</beans:bean>

I am using POSTMAN rest client chrome plugin for calling the webservice. JSON object passed is:

{"mark":30,"name":"sam","rollNo":100}

I am getting '415 Unsupported Media Type' as response when calling the web service.

Please help. Thanks in advance!

Upvotes: 0

Views: 7782

Answers (2)

Hatemii
Hatemii

Reputation: 49

Just Do This

@RequestMapping(value="/test",method=RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)

Upvotes: 0

geoand
geoand

Reputation: 64039

The problem is in the way you are invoking the Controller for the POSTMAN client.

It's missing the Content-Type: application/json HTTP Header

Upvotes: 6

Related Questions