I have been stuck on this problem for a very long time. I have looked on resources on the Internet but cannot find where I am going wrong. I have configured Spring MVC to send and receive JSON. When I invoke a RESTful service from the web browser for @ResponseBody the Object that is being returned is returned as JSON. However, when trying to invoke a @RequestBody I am unable to.
Below is the code:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<display-name>WebApp</display-name>
<context-param>
<!-- Specifies the list of Spring Configuration files in comma separated format.-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/history-service.xml</param-value>
</context-param>
<listener>
<!-- Loads your Configuration Files-->
<listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>history</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>history</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
history-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:component-scan base-package="com.web"/>
<mvc:annotation-driven/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageChanger"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>-->
Controller class
@Controller
@RequestMapping("/history/*")
public class ControllerI {
@RequestMapping(value = "save", method = RequestMethod.POST, headers = {"content- type=application/json"})
public @ResponseBody UserResponse save(@RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
return userResponse;
}
@RequestMapping(value = "delete", method = RequestMethod.GET)
public @ResponseBody UserResponse delete() {
System.out.println("Delete");
UserResponse userResponse = new UserResponse();
userResponse.setSuccess(true);
return userResponse;
}
When invoking /webapp/history/delete I can receive JSON.
Index.jsp
<%@page language="java" contentType="text/html"%>
<html>
<head>
</head>
<body>
<h2>WebApp</h2>
<form action="/webapp/history/save" method="POST" accept="application/json">
<input name="userId" value="Hello">
<input name="location" value="location">
<input name="emailAddress" value="hello@hello.com">
<input name="commitMessage" value="I">
<input type="submit" value="Submit">
</form>
</body>
</html>
However, when invoking the /save I get the following error:
org.springframework.web.servlet.mvc.support.DefaultHandlerE
xceptionResolver handleNoSuchRequestHandlingMethod
WARNING: No matching handler method found for servlet request: path '/history/sa
ve', method 'POST', parameters map['location' -> array<String>['location'], 'use
rId' -> array<String>['Hello'], 'emailAddress' -> array<String>['hello@hello.com'], 'commitMessage' -> array<String>['I']]
I am not sure where I am going wrong. All I want to do is send JSON through JSP to the Spring MVC Controller so the @RequestBody can be turned deserialized into Java from JSON.
I hope you can help.
chage accept=”application/json” to enctype=”application/json” on your HTML form.
Spring can not parse you post data for you did not set the current headers.
When use @RequestBody the default accept enctype is application/json.
try to one of this:
Edit
The enctype must the same as headers.
Ok, the code now looks like one of those:
Try one of those above.