Spring Mvc Rest Web服务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

查看:493
本文介绍了Spring Mvc Rest Web服务jstl表单提交HTTP状态415内容类型'application/x-www-form-urlencoded'不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring Mvc Rest Web服务与JSTL表单提交一起使用.

I am Using Spring Mvc Rest Webservice with JSTL form submittion.

<%@ 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>

这是我的控制器功能,可以接受请求.

Here is my controller function that accepts the request.

@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结合使用时,这可以很好地工作并节省员工

This works fine and saves the employee when i use it with ajax submit or with using RESTClient

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

The Error returns is.. HTTP Status 415: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

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

如何通过JSTL表单提交设置Mime类型以及如何解决该问题. 请有人帮忙.

How Can i set the Mime type with JSTL form submittion and how can i solve the problem. Please Somebody help.

推荐答案

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

When you're posting a form, the data are not sent as JSON, and since you have explicitly set that you're accepting only consumes=MediaType.APPLICATION_JSON_VALUE you get the 415 error. You can have all your cases covered by removing the @RequestBody and the consumes attribute, Spring MVC is smart enough to know what to do in all of your cases (form submition, RESTClient or ajax)

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

这篇关于Spring Mvc Rest Web服务jstl表单提交HTTP状态415内容类型'application/x-www-form-urlencoded'不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆