在REST请求中,可能会用到PUT类请求用来处理幂等类的更新,但是Spring MVC默认是无法解析PUT请求的参数的,拿到的参数值全部是null。那么如何令Spring MVC能够解析PUT请求的参数呢?
方案一:直接把参数放在URL中。Spring MVC是可以解析把参数放在URL的PUT请求的,但是通常情况下,PUT请求都会把内容放在body中,这样做的感觉很奇怪,而且不符合标准,不推荐使用。
方案二:利用Path variable,采用诸如/api/select/{id}的方式接收参数,代码如下:
@RequestMapping(value = "/api/select/{id}", method = RequestMethod.PUT)
public @ResponseBody select(@PathVariable("id") final Long id,
HttpServletResponse response) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("id", id);
return dataMap;
方案三:自Spring 3.1以后,Spring MVC提供了一个针对PUT参数问题的解决方案。即通过web.xml配置用以解决PUT参数问题的拦截器根治此类问题(推荐方案)。配置如下:
<filter>
<filter-name>httpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
&amp;amp;lt;!-- 解决PUT请求无法提交表单数据的问题 --&amp;amp;gt;
&amp;amp;lt;filter&amp;amp;gt;
&amp;amp;lt;filter-name&amp;amp;gt;HttpMethodFilter&am
Tomcat接收到POST请求时:
* 1.将请求数据封装一个Map
* 2.request.getParameter(“userName”)就会从Map中取值
* 3.SpringMVC封装POJO数据的时候,会把每个属性值调用request.getParameter(“userName”)
Tomcat接收到PUT请求时:
* 1.不会封装PUT请求...
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘PUT’ not supported]
1.在配置文件中添加(默认是关闭的)
spring.mvc.hiddenmethod.filter.enabled=true
2.th:href改为th:action
<form th:href="@{/emp}" method="post">
问题描述:
SpringBoot中更新表数据 ,发送put请求不起作用,控制台每次都是走post请求,最后我查找百度找到说是因为注解@ConditionalOnProperty限制了自动配置,默认false不开启配置,所以页面的put提交无法使用
<input type="hidden" name="_method" value="put" th:if="${emp!=null}">
解决方案:
SpringMVC使用get/post以外提交方式,例如put等需要具备以下条件:
请求参数和PUT方法(Request params and PUT method)我试图通过基于Grails的应用程序获取PUT请求传递的请求参数。我正在使用以下客户端代码发出请求:$.ajax({url: 'api/controllerName/anId',type: 'PUT',data: $('form').serialize()})与以下映射:"/api/$controller/$id?"...
在接口编写时,PUT方法请求时响应的数据为:{"message":"","statusCode":500}实际请求的数据为不难发现PUT data竟然为空为了排除是Java后台的问题,使用Postman发送该PUT请求,如下:说明Postman请求是没问题,那么基本可以排除是被测的Java后台的问题如果将参数直接写在url上,是可以请求成功的请求的数据如下:响应数据为:{"statusCode":2
1:get请求一般发送请求是这么写axios.get('/user?id=12345&name=user').then(function (res) {console.log(res);}).catch(function (err) {console.log(err);});但是为了方便全局统一调用封装的axiosaxios.get('/user', { //params参数必写 , 如...
添加一个过滤器:<filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mappi
目录:(1)设置在页面中支持Put和Delete请求 (2)REST请求url加上请求方式必须唯一浏览将其只支持Get和Post请求,不支持Put、和Daelete请求的(1)设置在页面中支持Put和Delete请求MyRestController:最后一个方法
配置文件:application.properties
testrest.html:
运行主启动类:Application
点击按钮发送put请求:因为method=“post” 不支持会报400 如果想要程序支持Put和Dele