F
F
Fresh_Trash2015-02-23 19:08:51
Java
Fresh_Trash, 2015-02-23 19:08:51

Not converting JSON to Java Object?

Hello.
The problem is, I'm sending JSON through the REST client and, in theory, in my code it should be automatically converted to a Java Object, but this does not happen, it gives out "415 Unsupported Media Type".
controller code: I tried to insert Content-Type into the header without a header. Tried writing produces = MediaType.APPLICATION_JSON_VALUE.

@RequestMapping(value = "/add", method = RequestMethod.POST,
                    headers = "Accept=application/json")
    @ResponseBody
    public User addUser(@RequestBody final User user) throws Exception {
        System.out.println("addUser: "+user);

      return null;
    }

user class code:
public class User implements Serializable {
    private Integer id;
    private String name;
    private String lastName;
    private String middleName;
    private Integer type;
    private String login;
    private String password;
//+ геттеры, сеттеры и констр. по умолчанию

web.xml
<web-app version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring MVC Application</display-name>

    <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="testservice"/>
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>   
</beans>

JSON:
{
"id" : 1,
"name" : "Ivan",
"lastName" : "Ivanov",
"middleName" : "Ivanovich",
"type" : 0,
"login" : "login",
"password" : "password"
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Simasik, 2015-02-23
@Simasik

Are you okay with types? Java is a strongly typed language and you cannot cast variables of one type to another.

V
Vladimir Smirnov, 2015-02-24
@bobzer

Try to add to the addUser method
and headers are not needed there

S
shizo, 2015-02-27
@shizo

c Content-Type: application/json in the header, everything went well. (Without it: 415 and Content type 'application/octet-stream' not supported). Posted by Fiddler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question