Answer the question
In order to leave comments, you need to log in
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;
}
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-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>
<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>
{
"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
Are you okay with types? Java is a strongly typed language and you cannot cast variables of one type to another.
Try to add to the addUser method
and headers are not needed there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question