我正在使用Spring Mvc Rest Webservice和JSTL表单提交。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page session="false" %>
<html>
<head>
</head>
<body>
<form:form method="POST" action="rest/save" modelAttribute="employee">
<table>
<tr><td>
id        <form:input type="text" path="id"/>
</td></tr>
<tr><td>
fname <form:input type="text" path="firstName"/>
</td></tr>
<tr><td>
lname <form:input type="text" path="lastName"/>
</td></tr>
<tr><td>
phone <form:input type="text" path="phone"/>
</td></tr>
</table>
<input type="submit" value="submit" >
</form:form>
这是我接受请求的控制器功能。
@RequestMapping(value = EmpRestURIConstants.SAVE_EMP,method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String setemployee(@RequestBody Employee employee){
dataServices.setemp(employee);
return "success";
}
当我使用ajax提交或使用RESTClient
时,这可以正常工作并保存员工错误返回是.. HTTP状态415:服务器拒绝此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported
如何使用JSTL表单提交设置Mime类型以及如何解决问题。 请有人帮忙。
答案 0 :(得分:1)
当您发布表单时,数据不会以JSON
的形式发送,并且由于您已明确设置为仅接受consumes=MediaType.APPLICATION_JSON_VALUE
,因此您获得了{{1}错误。您可以通过删除415
和@RequestBody
属性来涵盖所有案例,Spring MVC足够聪明,可以知道您在所有情况下要做什么(表单submition,RESTClient或ajax)
consumes