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应用程序中,接 ...
随机推荐
- Ubuntu下设置VNCServer
Ubuntu下设置VNCServer Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作.服 ...
- 如何边遍历集合边删除元素--使用Iterator中的remove()方法
在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.
错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...
- linux 创建docker基础镜像
通过Dockerfile创建镜像时,一般都是基于 Docker Hub 提供的官方镜像.以下分别介绍在ubuntu16和centos7 两个系统上创建个人私有基础镜像的方法. 一.ubuntu16创 ...
- 使用Javascript Ajax 通信操作JSON数据 [下]
上一篇文章我们获得后台数据库的数据后转换成json格式然后返回到前台,但只是返回的一位数组,这次我们返回二维和三维数组和对象. 前台代码shizhan.html: <!DOCTYPE html& ...
- ReactNative安装配置
1.安装jdk1.8,配置好path, javac,java -version 2.安装设置Android sdk a. 解压:D:\www\sdk\adt-bundle-windows-x86_64 ...
- wenfrom的简单控件和repeater控件
简单控件 lable 转换成<span>标记 literal 空的 什么也没转换 Literal.Text=<script>alter('你好');</scrip ...
- php学习笔记-do while循环
do{ func(); }while(condition) do while执行逻辑是先执行循环体里面的代码,再判断condition是否为true,如果是则和while循环一样了.如果conditi ...
- 算法Sedgewick第四版-第1章基础-003一封装日期
1. package ADT; import algorithms.util.StdOut; /**************************************************** ...
- HBase 协处理器统计行数
环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...