HTTP Status 415 – Unsupported Media Type(使用@RequestBody后postman调接口报错)
- 问题:使用SpringMVC框架后,接口中入参对象没使用@RequestBody注解,造成postman发起post请求,from-data格式请求可以调通接口,但是raw格式请求调不通接口,然后我加了SpringMVC @ResponseBody 注解,调接口显示415错误。
- 处理:
- SpringMVC添加配置、注解:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
- pom.xml添加jackson包引用:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
- Ajax请求时没有设置Content-Type为application/json。
- 注释:按照上面配置完成后,可以使用raw调用接口中包含@RequestBody注解的接口,但是此时你会发现form-data这种方式访问接口又调不通了,百度了下,解决如下:
- form-data 的 Content-Type 的类型是 application/x-www-form-urlencoded,是表单编码,不同的是还可以提交文件。
- raw 的话,有几种,常用的是application/json
- 如果SpringMVC要接受application/json,需要使用 @RequestBody 注解来接收。
- 也就是说 @RequestBody 注解接收的是application/json格式的参数(json对象),这种参数使用raw传参可以自定义格式为(JSON(application/json)),但是form-data传参的格式是application/x-www-form-urlencoded的,所以调不通接口。
- 参考:
- 总结:
- Content-Type为:application/x-www-form-urlencoded 后端接口正常不处理可以调用,传递的是JSON对象(Ajax中的data),也可以使用@RequestParam注解去获取表单中的值。
- Content-Type为:application/json 后端接口入参得用@RequestBody 注解来接收,传递的JSON对象的字符串,传递的不是JSON对象。
- springMVC 如果不进行必要配置的话,只支持x-www-form-urlencoded,如果使用form-data会报400 Bad Request错误。
在spring的mvc配置文件中配置对multi的支持即可,如下: <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean> 此时对以上两种编码格式都支持。 注意:此时需要引入
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
HTTP Status 415 – Unsupported Media Type(使用@RequestBody后postman调接口报错)的更多相关文章
- request 报错The remote server returned an error: (415) Unsupported Media Type.
开发时遇到个问题,程序访问数据库数据,给服务器发送请求时,老是报错,返回的错误页面是: HTTP Status 415 - Unsupported Media Type type Status rep ...
- springMVC中使用 RequestBody 及 Ajax POST请求 415 (Unsupported Media Type)
使用POST请求的时候一直报错: Ajax 未设置 contentType 时会报 415 . 后台 RequestBody 承接前台参数,故对参数data的要求为“必传”“JSON”,否则会报40 ...
- angularjs - 415 (Unsupported Media Type)
angularJs+springMVC angular表单提交一个user实体时,报 angularjs - 415 (Unsupported Media Type)错误!! 原因是$http({ u ...
- SpringMVC 中HttpMessageConverter简介和Http请求415 Unsupported Media Type的问题
一.概述: 本文介绍且记录如何解决在SpringMVC 中遇到415 Unsupported Media Type 的问题,并且顺便介绍Spring MVC的HTTP请求信息转换器HttpMessag ...
- 415 Unsupported Media Type
415 Unsupported Media Type - HTTP | MDN https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415
- System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...
- RestTemplate远程调用POST请求:HTTP 415 Unsupported Media Type
这是本项目的接口 称为client @POST @Path("/{urlcode}") @Consumes(MediaTypes.JSON_UTF_8) @Produces(Med ...
- Darwin Streaming Server服务器mp4文件点播返回”415 Unsupported Media Type“错误
Darwin Streaming Server中mp4文件点播失败,通过抓包发现服务器返回”415 Unsupported Media Type“错误,如下: RTSP/ Unsupported Me ...
- [.NET Core]ASP.NET Core中如何解决接收表单时的不支持的媒体类型(HTTP 415 Unsupported Media Type)错误呢?
[.NET Core]ASP.NET Core中如何解决接收表单时的不支持的媒体类型(HTTP 415 Unsupported Media Type)错误呢? 在ASP.NET Core应用程序中,接 ...
随机推荐
- SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数
数据库中的函数和c#中的函数很相似 按顺序来, 这里价格特别的 print 可以再消息栏里打印东西 数学函数 ceiling() 取上限 不在乎小数点后面有多大,直接忽略 floor() ...
- 机器人自主移动的秘密,从SLAM技术说起(一)
博客转载自:https://www.leiphone.com/news/201609/c35bn1M9kgVaCCef.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...
- noi.ac day1t1 candy
传送门 分析 我们知道如果设A,B分别为将两家店从大到小排序之后各自的前缀和,则 Ans=Max{Min{A[i],B[j]}-W*(i+j)}. 为了得到这个Ans我们可以枚举两个数的Min,然后剩 ...
- notpad++ 开发php神奇
开发PHP应具有的插件: 1. Compare: 可以用来比较两个文件不同之处. 2. Explorer:文件浏览器插件,包含收藏夹.Session保存功能.可与NppExec脚本结合使用. 3. ...
- 动态tab页
1.前台代码 <%-- builed by manage.aspx.cmt [ver:2015.25.26] at 2015-06-26 15:25:42 --%> <%@ Pag ...
- 数据结构_Summary
问题描述 可怜的 Bibi 丢了好几台手机以后,看谁都像是小偷,他已经在小本本上记下了他认为的各个地点的小偷数量.现在我们将 Bibi 的家附近的地形抽象成一棵有根树. 每个地点都是树上的一个节点,节 ...
- HUST高级软件工程--测试管理工具实践--Day1
测试管理工具实践--Day1 今天完成任务情况: 课前组好队伍,建好微信群. 课上通过老师的介绍,初步了解各种测试工具的使用情况. 课后选取了组长,在微信群经过"广泛而激烈"的讨论 ...
- Java50道经典习题-程序49 子串出现的个数
题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数分析:例如输入的字符串为"I come from County DingYuan Province AnHui.&quo ...
- Dapper相关了解
公司新项目用的是Dapper,做的时候没有具体看dapper的具体用法,现在回来回顾总结一下. 1-总体介绍dapper 我们都知道ORM全称叫做Object Relationship Mapper, ...
- Codeforces 917B MADMAX (DP+博弈)
<题目链接> 题目大意:给定一个DAG图,其中图的边权是给定的字符所对应的ascii码,现在A先手,B后手,每次沿DAG图走一步,但是第i次走的边权一定要大于等于第i-1次走的边权(这里是 ...