如果您对POSTJSON失败,出现415Unsupportedmediatype,Spring3mvc和postmanjsonparseerror感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解
如果您对POST JSON 失败,出现 415 Unsupported media type, Spring 3 mvc和postman json parse error感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解POST JSON 失败,出现 415 Unsupported media type, Spring 3 mvc的各种细节,并对postman json parse error进行深入的分析,此外还有关于$.ajax访问RESTful Web Service报错:Unsupported Media Type、415 Spring应用程序中不支持POST请求的MediaType、415 Unsupported Media Type、415 unsupported media type拒绝请求怎么解决的实用技巧。
本文目录一览:- POST JSON 失败,出现 415 Unsupported media type, Spring 3 mvc(postman json parse error)
- $.ajax访问RESTful Web Service报错:Unsupported Media Type
- 415 Spring应用程序中不支持POST请求的MediaType
- 415 Unsupported Media Type
- 415 unsupported media type拒绝请求怎么解决
POST JSON 失败,出现 415 Unsupported media type, Spring 3 mvc(postman json parse error)
我正在尝试向 servlet 发送 POST 请求。请求是通过 jQuery 以这种方式发送的:
var productCategory = new Object();productCategory.idProductCategory = 1;productCategory.description = "Descrizione2";newCategory(productCategory);
newCategory 在哪里
function newCategory(productCategory){ $.postJSON("ajax/newproductcategory", productCategory, function( idProductCategory) { console.debug("Inserted: " + idProductCategory); });}
而 postJSON 是
$.postJSON = function(url, data, callback) { return jQuery.ajax({ ''type'': ''POST'', ''url'': url, ''contentType'': ''application/json'', ''data'': JSON.stringify(data), ''dataType'': ''json'', ''success'': callback });};
使用 firebug,我看到 JSON 发送正确:
{"idProductCategory":1,"description":"Descrizione2"}
但我得到 415 Unsupported media type。Spring mvc 控制器具有签名
@RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)public @ResponseBodyInteger newProductCategory(HttpServletRequest request, @RequestBody ProductCategory productCategory)
前几天有效,现在无效。如果需要,我会显示更多代码。
答案1
小编典典我设法使它工作。告诉我,以防我错了。@JSONSerialize
我只使用了一种方法来序列化/反序列化:我删除了关于这个
(和)的所有注释,并在课堂上@JSONDeserialize
注册了序列化器和反序列化器。CustomObjectMapper
我没有找到解释这种行为的文章,但我以这种方式解决了。希望它有用。
$.ajax访问RESTful Web Service报错:Unsupported Media Type
最近在项目中,前台页面使用jquery ajax访问后台CXF发布的rest服务,结果遇到了错误"Unsupported Media Type"。
发布的服务java代码如下:
import javax.jws.WebService; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; @WebService @Produces({ "application/json" }) public class TrackService { @POST @Path("/trackInBatch/") @Consumes("application/json") public Response postTrackInfoInBatch(List<TrackPosition> positions) { return retrieve(positions,clientGen,trafficMapLayerId,projectParaLayerId,"0"); } }
调用服务的javascript代码如下:
$.ajax({ url : "/myapp/rest/track/trackInBatch/",async:false,type : "POST",dataType:"json",data:[],error:function(XMLHttpRequest,textStatus,errorThrown){ alert(errorThrown); },success: function(data,textStatus){ outResponse = data; } });
调用的服务的时候报错:Unsupported Media Type。通过HttpWatch查看原始的request和response报文,发现返回request报文中的contentType是:application/x-www-form-urlencoded。查看jquery.ajax()的api文档,发现contentType的默认值就是:application/x-www-form-urlencoded。
但是后台发布的rest服务,@Consumes("application/json")要求request报文的contentType必须是application/json。
手动设置contentType之后,发现问题解决。
$.ajax({ url : "/myapp/rest/track/trackInBatch/",contentType:"application/json",textStatus){ outResponse = data; } });
415 Spring应用程序中不支持POST请求的MediaType
我有一个非常简单的Spring
应用程序(没有Spring Boot)。我已经实现了GET
和POST
控制器方法。该GET
方法效果很好。但是,POST正在抛出415 Unsupported MediaType
。复制步骤如下
ServiceController. java
package com.example.myApp.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/service/example") public class ServiceController { @RequestMapping(value="sample", method = RequestMethod.GET) @ResponseBody public String getResp() { return "DONE"; } @RequestMapping(value="sample2", method = RequestMethod.POST, consumes = "application/json") @ResponseBody public String getResponse2(@RequestBody Person person) { return "id is " + person.getId(); }}class Person { private int id; private String name; public Person(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
AppConfig.java
package com.example.myApp.app.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration@EnableWebMvc@ComponentScan("com.example.myApp")public class AppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/test/**").addResourceLocations("/test/").setCachePeriod(0); registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0); registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0); registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0); }}
AppInitializer.java
package com.example.myApp.app.config;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.ContextLoaderListener;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { // Create the ''root'' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }}
该代码在这里可用:
git clone https://bitbucket.org/SpringDevSeattle/springrestcontroller.git./gradlew clean build tomatrunwar
这将旋转嵌入式tomcat。
现在你可以卷曲以下内容
curl -X GET -H "Content-Type: application/json" "http://localhost:8095/myApp/service/example/sample"
工作良好
但
curl -X POST -H "Content-Type: application/json" ''{ "id":1, "name":"sai"}'' "http://localhost:8095/myApp/service/example/sample2"
引发415不支持的MediaType
<body> <h1>HTTP Status 415 - </h1> <HR size="1" noshade="noshade"> <p> <b>type</b> Status report </p> <p> <b>message</b> <u></u> </p> <p> <b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u> </p> <HR size="1" noshade="noshade"> <h3>Apache Tomcat/7.0.54</h3> </body>
答案1
小编典典我找到了解决方案,因此我想在这里发布,以使他人受益。
首先,我需要在我的类路径中包含杰克逊,我在build.gradle中添加了以下内容:
compile ''com.fasterxml.jackson.core:jackson-databind:2.7.5'' compile ''com.fasterxml.jackson.core:jackson-annotations:2.7.5'' compile ''com.fasterxml.jackson.core:jackson-core:2.7.5''
接下来,我必须更改AppConfig
其扩展范围WebMvcConfigurerAdapter
,如下所示:
@Configuration@EnableWebMvc@ComponentScan("com.example.myApp")public class AppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/test/**").addResourceLocations("/test/").setCachePeriod(0); registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0); registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0); registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter()); super.configureMessageConverters(converters); }}
就这样,一切都很好
415 Unsupported Media Type
今天使用 post 提交表单时显示
"timestamp": "2018-10-25T01:21:54.152+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type ''application/x-www-form-urlencoded;charset=UTF-8'' not supported",
然后看了下,因为是 post 表单提交的,所以 Content-Type 是 application/x-www-form-urlencoded
查看服务端代码
@RequestMapping(value ="save" , method = RequestMethod.POST)
@ResponseBody
public boolean productSave(@RequestBody Product product){
return productService.saveProduct(product);
}
去掉参数里的 @RequestBody 就可以成功
415 unsupported media type拒绝请求怎么解决
415 unsupported media type 错误表示服务器无法处理客户端提供的媒体类型。解决方法包括:检查请求媒体类型是否与服务器期望的类型匹配。查看服务器支持的媒体类型。更新客户端应用程序以发送正确的媒体类型。利用媒体类型协商让客户端指定支持的类型。转换请求内容到服务器支持的媒体类型。
415 Unsupported Media Type 错误:如何解决
415 Unsupported Media Type 错误表明服务器无法处理客户端请求中提供的媒体类型。这通常意味着请求包含服务器不识别的格式或编码。
最近大家都在看
http请求415错误解决方法
解决Python报错:TypeError: unsupported operand type(s) for +: ''str'' and ''int''
Java中的UnsupportedClassVersionError异常的解决方法
解决方法:
1. 检查请求中的媒体类型:
-
确保请求中指定的 Content-Type 标头与服务器期望的媒体类型相匹配。常见错误包括:
- 设置错误的 Content-Type
- 无法识别文件扩展名
- 缺少 Content-Type 标头
2. 检查服务器支持的媒体类型:
- 查阅服务器的文档或联系服务器管理员以确定支持的媒体类型。
- 常见支持的媒体类型包括:"application/json"、"application/xml"、"text/plain"、"text/html"
3. 更新客户端应用程序:
- 确保客户端应用程序正确配置为发送服务器期望的媒体类型。
- 更新应用程序以支持最新的媒体类型规范。
4. 使用媒体类型协商:
- 如果服务器支持媒体类型协商,则客户端可以发送 Accept 标头,其中包含客户端支持的媒体类型。
- 服务器将选择它支持的优先级最高的媒体类型。
5. 转换请求内容:
- 如果无法直接更新客户端应用程序,则可以考虑将请求内容转换为服务器支持的媒体类型。
- 例如,使用第三方库或工具将 JSON 转换为 XML。
其他提示:
- 检查服务器日志以获取有关错误的更多详细信息。
- 使用开发者工具(例如 Chrome DevTools)来检查请求和响应头。
- 确保服务器正确配置为处理客户端请求的媒体类型。
以上就是415 unsupported media type拒绝请求怎么解决的详细内容,更多请关注php中文网其它相关文章!
关于POST JSON 失败,出现 415 Unsupported media type, Spring 3 mvc和postman json parse error的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于$.ajax访问RESTful Web Service报错:Unsupported Media Type、415 Spring应用程序中不支持POST请求的MediaType、415 Unsupported Media Type、415 unsupported media type拒绝请求怎么解决的相关知识,请在本站寻找。
本文标签: