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

这篇具有很好参考价值的文章主要介绍了【springboot中使用@RequestBody MultiValueMap 总是报400,415错误-关于流程引擎flowable】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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-7316

		this.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 的处理过程,我们把它加上文章来源地址https://www.toymoban.com/news/detail-583400.html

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    @Bean
    public 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;
    }
}

到了这里,关于【springboot中使用@RequestBody MultiValueMap 总是报400,415错误-关于流程引擎flowable】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • http 415 错误

    HTTP响应返回415状态码,错误信息为“Unsupported Media Type”,也就是服务器无法处理请求附带的媒体格式。比如服务端可接收的格式为json,客户端发出的报文header 中”content-type“为text。 解决方法两个,一个是不要设置服务端仅接收某一非默认类型,如json,另一个是客户端补充

    2024年02月12日
    浏览(20)
  • HTTP 请求 400错误

    HTTP 请求 400错误 客户端发送请求 服务端接收请求 客户端控制台打印 服务端控制台打印 将 \\\"avatarBase64\\\"\\\" 修改为 \\\"avatarBase64\\\": \\\"\\\" 即修改请求体: 为 产生这个问题的原因是客户端发送的 JSON 请求体格式不正确,导致服务端无法解析请求。具体地说,在客户端构建请求体时,键

    2024年02月19日
    浏览(33)
  • 系统测试——postman的400错误

    如果Headers中不勾选Host,调用接口就会报400 Bad Request错误。

    2024年02月13日
    浏览(29)
  • HTTP代理出现400错误的原因及解决办法

           在使用HTTP代理过程中,会经常出现各种代码错误的提示,以下是使用HTTP代理出现400代码的原因和解决办法      使用HTTP代理时,出现400 Bad Request错误代码通常表示客户端发送的请求格式不正确或包含了无效的参数。下面是一些可能导致400错误的原因: 请求参数错误

    2024年02月06日
    浏览(36)
  • Vue2 axios 发请求报400错误 “Error: Request failed with status code 400“

    最近在做一个项目,后端写了一个登录接口,postman可以正常请求。但我axios发请求出现400错误 \\\"Error: Request failed with status code 400\\\" 请求: 错误:  我研究了两天,查了无数资料,最后和朋友一起找到解决方法 原因:后端没跟我说需要什么格式的数据,我以为是默认的json,后来

    2024年02月11日
    浏览(56)
  • 网络连接不上总是出现651错误怎么办?如何解决?

    随着电脑的普及,几乎每家每户都有一台甚至多台电脑,而购买电脑之后的第一件事情就是装宽带,使电脑的功效发挥到最大化,但是很多朋友都遇到网络连接不上总是出现651的错误,那么怎么办呢?下面小编就为大家详细介绍一下,有需要的朋友可以参考一下 1、单击左下

    2024年02月08日
    浏览(56)
  • Java 发送Http请求携带中文参数时 请求报400的错误请求

    在 Java 中,URL 中不能直接包含中文字符,因为 URL 规范要求 URL 必须是 ASCII 字符。如果需要在 URL 中传递中文参数,需要对中文参数进行 URL 编码,将其转换为浏览器中的参数形式。可以使用 java.net.URLEncoder 类来进行 URL 编码。

    2024年02月11日
    浏览(32)
  • 解决SpringBoot中@RequestBody不能和Multipart同时传递的问题

    今天在做文件上传的时候,遇到了这么一个错误日志: Resolved[org.springframework.web.HttpMediaTypeNotSupportedException: Content type ‘multipart/form-data;boundary=--------------------------771899451541318130280588;charset=UTF-8’ not supported] 从日志中可以发现,好像是因为@RequestBody不能和MultipartFile一起使用。

    2024年01月25日
    浏览(31)
  • Android studio 设置proxy错误,导致HTTP/1.1 400 Bad Request。

    Android studio 设置proxy 大家在打开一个新项目是,往往需要下载组件和插件。如果出现一下错误信息:     java.io.IOException: Unable to tunnel through proxy. Proxy returns \\\"HTTP/1.1 400 Bad Request\\\" Failed to download any source lists!  原因是连接下载源失败。Proxy returns \\\"HTTP/1.1 400 Bad Request代理失败。 请

    2024年02月05日
    浏览(95)
  • Postman发送post请求时报400错误,Required request body is missing

    项目形参位置存在@RequestBody注解,用Postman发送post请求时报400错误,Required request body is missing。 错误图示: 解决方法: 方法一: 项目中形参位置不使用@RequestBody,在Postman进行Post请求时,在请求路径后直接拼接参数。 方法二: 项目中形参位置使用@RequestBody,在Postman进行Po

    2024年02月11日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包