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应用程序中,接 ...
随机推荐
- 第三章 Java内存模型(下)
锁的内存语义 中所周知,锁可以让临界区互斥执行.这里将介绍锁的另一个同样重要但常常被忽视的功能:锁的内存语义 锁的释放-获取建立的happens-before关系 锁是Java并发编程中最重要的同步机 ...
- Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()
转自:https://blog.csdn.net/l1028386804/article/details/65449355 ava.lang.NoSuchMethodException: [org.s ...
- UIView显示原理和过程
一.UIView显示原理 一个控件,UIView之所以可以显示,是因为内部在UIView的内部有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内 ...
- 【总结整理】arcgis js api的Map类
关于ArcGis for javascrept之Map类 ArcGis for javascrept_ESRI_Map类: 1. 构造方法:esri.Map(); 参数: extent 如果设置 ...
- 2018网络预选赛 徐州G 线段树
线段树,假设求(x1,y1)点的贡献,就找所有比该点出现时间晚,且x坐标大于x1的点中y最大的,贡献就是Y-y1,由于题目条件限制,不可能有x坐标大于(x1,y1)且y坐标大于y1的点,所以贡献肯定为 ...
- 15-struct(构造函数,重载)
必须充分掌握struct的使用,包括其构造和重载函数的写法: #include <iostream> using namespace std; struct node { int x, y ...
- ZBar开发详解
博客转载自:https://blog.csdn.net/skillcollege/article/details/38855023 什么是ZBar? ZBar是一个开源库,用于扫描.读取二维码和条形码 ...
- CMake 默认编译、链接选项
查看cmake默认编译和链接的参数设置 CMakeLists.txt 文件内容: cmake_minimum_required(VERSION 3.2) message(STATUS "CM ...
- Libcurl 简明使用指南
http://blog.csdn.net/weiling_shen/article/details/7620397 我们的程序中使用的是人家的Easy.h头文件.....完成文件的上传和下传. 尤其是 ...
- 创建Mat对象
Mat 是一个非常优秀的图像类,它同时也是一个通用的矩阵类,可以用来创建和操作多维矩阵.有多种方法创建一个 Mat 对象. 1.构造函数方法 下面是一个使用构造函数创建对象的例子. 常用的构造函数 2 ...