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;}
}