我一直在这个问题上停留很长时间。我查看了Internet上的资源,但找不到我要去哪里。我已经配置了Spring MVC来发送和接收JSON。当我从Web浏览器为@ResponseBody调用RESTful服务时,将返回的对象作为JSON返回。但是,当尝试调用@RequestBody时,我无法执行。
下面是代码:
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
@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;
}
调用/ webapp / history / delete时,我可以接收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>
但是,调用/ save时,出现以下错误:
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']]
我不确定我要去哪里。我要做的就是通过JSP将JSON发送到Spring MVC Controller,以便可以将@RequestBody从JSON反序列化为Java。
希望您能提供帮助。
最佳答案
更改HTML表单上的accept =“ application / json”到enctype =“ application / json”。
Spring无法解析您的发布数据,因为您没有设置当前标头。
使用@RequestBody时,默认的接受enctype为application / json。
尝试以下之一:
在您的html表单或js帖子功能上设置帖子标头=“ content-type = application / json”。
将您的控制器设置为接受标头=“ content-type = application / x-www-form-urlencoded”。
编辑
编码类型必须与标题相同。
好的,代码现在看起来像其中之一:
// the one
@RequestMapping(value = "save", method = RequestMethod.POST)
public @ResponseBody UserResponse save(@RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
return userResponse;
}
</form>
<form action="/webapp/history/save" method="POST" enctype="application/x-www-form-urlencoded">
<input name="userId" value="user">
<input name="location" value="location">
<input name="emailAddress" value="hello@hello.com">
<input name="commitMessage" value="I">
<input type="submit" value="Submit">
</form>
// the other
@RequestMapping(value = "save", method = RequestMethod.POST, headers = {"content-type=application/x-www-form-urlencoded"})
public @ResponseBody UserResponse save(@RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
return userResponse;
}
</form>
<form action="/webapp/history/save" method="POST" enctype="application/json">
<input name="userId" value="user">
<input name="location" value="location">
<input name="emailAddress" value="hello@hello.com">
<input name="commitMessage" value="I">
<input type="submit" value="Submit">
</form>
尝试上述方法之一。
我收到以下错误:
HTTP错误415:服务器拒绝了此请求,因为该请求
实体采用的格式不受请求的资源支持
请求的方法()。