0

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!

2
  • Looks like your JSON string is wrong. See the quotes Commented Jun 16, 2014 at 7:45
  • sorry.By mistake I have written here like that.I have edited it. Commented Jun 16, 2014 at 7:49

2 Answers 2

6

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

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

Comments

0

Just Do This

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.