Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC 3 JSON
    text
    copied!<p>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.</p> <p>Below is the code:</p> <p>web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;display-name&gt;WebApp&lt;/display-name&gt; &lt;context-param&gt; &lt;!-- Specifies the list of Spring Configuration files in comma separated format.--&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/history-service.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;!-- Loads your Configuration Files--&gt; &lt;listener- class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;history&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;history&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; </code></pre> <p></p> <p>history-service.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans&gt; &lt;context:component-scan base-package="com.web"/&gt; &lt;mvc:annotation-driven/&gt; &lt;context:annotation-config/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/&gt; &lt;bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;property name="supportedMediaTypes" value="application/json"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;ref bean="jacksonMessageChanger"/&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;!-- &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt; &lt;property name="mediaTypes"&gt; &lt;map&gt; &lt;entry key="json" value="application/json"/&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt;--&gt; </code></pre> <p></p> <p>Controller class</p> <pre><code> @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; } </code></pre> <p>When invoking /webapp/history/delete I can receive JSON.</p> <p>Index.jsp</p> <pre><code> &lt;%@page language="java" contentType="text/html"%&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;WebApp&lt;/h2&gt; &lt;form action="/webapp/history/save" method="POST" accept="application/json"&gt; &lt;input name="userId" value="Hello"&gt; &lt;input name="location" value="location"&gt; &lt;input name="emailAddress" value="hello@hello.com"&gt; &lt;input name="commitMessage" value="I"&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>However, when invoking the /save I get the following error:</p> <pre><code>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' -&gt; array&lt;String&gt;['location'], 'use rId' -&gt; array&lt;String&gt;['Hello'], 'emailAddress' -&gt; array&lt;String&gt;['hello@hello.com'], 'commitMessage' -&gt; array&lt;String&gt;['I']] </code></pre> <p>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.</p> <p>I hope you can help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload