【springboot中使用@RequestBody MultiValueMap 总是报400,415错误-关于流程引擎flowable】

news/2025/3/21 23:33:27/

springboot中使用@RequestBody MultiValueMap 总是报400,415错误-关于流程引擎flowable

第一步:报错代码举例

在Spring boot 中使用 @RequestBody 会报错,提示错误 Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported,代码如下:

@RequestMapping(value = "/act/service/model/{modelId}/save", method = RequestMethod.POST)
public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values) {// 具体业务处理逻辑代码
}

第二步:报错原因分析

这个在传统 spring MVC 中是有效的,但是在 Spring boot 中会报错。

传统是 Spring MVC 有效,是因为有 mvc:annotation-driven 注解,查资料,mvc:annotation-driven 注解配置了如下的内容
spring 3.1 版本:

这个找到的资料是 3.1 的,

但是我们注意下面最后一行配置
但是我们注意下面最后一行配置

<!-- 注解请求映射  --><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        <property name="interceptors"><list>  <ref bean="logNDCInteceptor"/>   <!-- 日志拦截器,这是你自定义的拦截器 --></list>        </property>        </bean>      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters">  <list>  <ref bean="byteArray_hmc" />  <ref bean="string_hmc" />  <ref bean="resource_hmc" />  <ref bean="source_hmc" />  <ref bean="xmlAwareForm_hmc" />  <ref bean="jaxb2RootElement_hmc" />  <ref bean="jackson_hmc" />  </list>  </property>  </bean>  <bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 处理.. --><bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!-- 处理.. --><bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 处理.. --><bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 处理.. --><bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 处理.. --><bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 处理.. --><bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 处理json-->

第三步:找到问题

但是我们注意上面最后一行配置
但是我们注意上面最后一行配置
但是我们注意上面最后一行配置

最后一个配置了 Jackson 的 json 处理程序,
在更新的版本中,AnnotationMethodHandlerAdapter 已经废弃,
使用的是 RequestMappingHandlerAdapter,
看下 RequestMappingHandlerAdapter 的源码。
public RequestMappingHandlerAdapter() {StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316this.messageConverters = new ArrayList<HttpMessageConverter<?>>(4);this.messageConverters.add(new ByteArrayHttpMessageConverter());this.messageConverters.add(stringHttpMessageConverter);this.messageConverters.add(new SourceHttpMessageConverter<Source>());this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());}

第四步:解决方法-增加配置类增加对应数据格式的处理Bean

这里面没有了 json 的处理过程,我们把它加上

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {@Beanpublic RequestMappingHandlerAdapter requestMappingHandlerAdapter() {RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();MediaType textMedia = new MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8"));supportedMediaTypes.add(textMedia);MediaType jsonMedia = new MediaType(MediaType.APPLICATION_JSON, Charset.forName("UTF-8"));supportedMediaTypes.add(jsonMedia);jsonConverter.setSupportedMediaTypes(supportedMediaTypes);converters.add(jsonConverter);adapter.setMessageConverters(converters);return adapter;}
}
文章来源:https://blog.csdn.net/weixin_44188105/article/details/131701127
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ppmy.cn/news/863293.html

相关文章

axios ( ajax pro )

axios 什么是 axiosaxios 介绍axios 的使用axios 工具包下载axios 使用实例什么是 axios axios 介绍 根据我的理解就是对于原生ajax的一个封装,以达到方便使用异步刷新的目的。 ajax是什么呢?(Asynchronous JavaScript And XML,异步的JavaScript和XML) 首先要理解异步刷…

【无标题】打印水仙花(pyth)

i100 while i<1000: ai//100#计算百位 bint((i-a*100)/10)#计算十位 ci%10#计算个位 ta**3b**3c**3#用来判断是否为水仙花数 if ti: print(i) i1#继续进行循环

Python编程小练习——水仙花数

打印出所有的"水仙花数"&#xff0c;所谓"水仙花数"是指一个三位数&#xff0c; 其各位数字立方和等于该数本身。 例如&#xff1a;153是一个"水仙花数"&#xff0c;因为1531的三次方&#xff0b;5的三次方&#xff0b;3的三次方。 for i in ran…

python水仙花数判断程序

水仙花数判断程序 水仙花数是一个三位整数&#xff0c;如153是一个水仙花数&#xff0c;是因为该数 的百位的立方、十位的立方、个位的立方之和等于该数本身&#xff0c;如下所 示&#xff1a; 1^3 5^3 3^3153 print("所有的三位数中的水仙花数如下图所示&#xff1a;&q…

Python 寻找水仙花数

寻找水仙花数 寻找水仙花数 &#xff08;模块&#xff1a;numEx&#xff0c;所在文件名 num_hw.py&#xff0c;Level&#xff1a;★&#xff09; 水仙花数&#xff08;Narcissistic number&#xff09;是指一个 3 位数&#xff0c;它的每个位上的数字的 3 次幂之和 等于它本身…

Python判断水仙花数

水仙花数 水仙花数&#xff08;Narcissistic number&#xff09;也被称为超完全数字不变数&#xff08;pluperfect digital invariant, PPDI&#xff09;、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数&#xff08;Armstrong number&#xff09;&#xff0c;水仙花数是指一个 3 位…

python判断水仙花数,打印水仙花数

题目&#xff1a;水仙花数 #水仙花数是指一个n位数&#xff0c;N>3,它的每个位上的数的N次幂之和等于它本身 #例子&#xff1a;13533^3153 #求100-999范围内的&#xff0c;水仙花数 python 代码获取100-999范围内的&#xff0c;水仙花数 for num in range(100,1000):S_di…

python画板——画樱花

python画板——画樱花 话不多说&#xff0c;直接上源码&#xff0c;代码如下 import turtle as T import random import time# 画樱花的躯干(60,t) def Tree(branch, t):time.sleep(0.0005)if branch > 3:if 8 < branch < 12:if random.randint(0, 2) 0:t.color(sn…