Spring Mvc Rest Webservice jstl 表单提交 HTTP 状态 415 内容类型 'application/x-www-form-urlencoded' 不支持

Spring Mvc Rest Webservice jstl form submittion HTTP Status 415 Content type 'application/x-www-form-urlencoded' not supported

我正在使用 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 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<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

一起使用时,这工作正常并节省了员工

错误 returns 是.. HTTP 状态 415:服务器拒绝此请求,因为请求实体的格式不受所请求方法所请求资源的支持。

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported

如何使用 JSTL 表单提交设置 Mime 类型以及如何解决问题。 请有人帮助。

当您发布表单时,数据不会作为 JSON 发送,并且由于您已明确设置您只接受 consumes=MediaType.APPLICATION_JSON_VALUE,所以您会收到 415错误。您可以通过删除 @RequestBodyconsumes 属性来涵盖所有案例,Spring MVC 足够聪明,知道在您的所有案例(表单提交、RESTClient 或 ajax)

@RequestMapping(value = EmpRestURIConstants.SAVE_EMP,method = RequestMethod.POST)
public @ResponseBody String setemployee(Employee employee){
    dataServices.setemp(employee);
        return "success";   
}