415 Spring应用程序中不支持POST请求的MediaType

编程入门 行业动态 更新时间:2024-10-23 03:19:01
本文介绍了415 Spring应用程序中不支持POST请求的MediaType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个非常简单的Spring应用程序(没有弹簧启动).我已经实现了GET和POST控制器方法. GET方法可以正常工作.但是POST抛出415 Unsupported MediaType.复制步骤如下所示:

I have a very simple Spring Application (NOT spring boot). I have implemented a GET and POST controller methods. the GET method works fine. But the POST is throwing 415 Unsupported MediaType. Steps to reproduce are available below

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 bitbucket/SpringDevSeattle/springrestcontroller.git ./gradlew clean build tomatrunwar

这将旋转嵌入式tomcat.

This spins up embedded tomcat.

现在您可以卷曲以下内容

Now you can curl the following

curl -X GET -H "Content-Type: application/json" "localhost:8095/myApp/service/example/sample"

工作正常

但是

curl -X POST -H "Content-Type: application/json" '{ "id":1, "name":"sai" }' "localhost:8095/myApp/service/example/sample2"

抛出415不支持的MediaType

Throws 415 unsupported 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>

推荐答案

我找到了解决方案,我想在这里发布,以使其他人受益.

I found the solution and I want to post here so it benefits others.

首先,我需要在我的类路径中包含杰克逊,我将其添加到build.gradle中,如下所示:

Firstly I need to include jackson in my classpath, which I added in build.gradle as follows:

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'

接下来,我必须更改对WebMvcConfigurerAdapter进行扩展的AppConfig,如下所示:

Next, I have to change my AppConfig which extends WebMvcConfigurerAdapter as follows:

@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 Spring应用程序中不支持POST请求的MediaType

本文发布于:2023-10-29 10:09:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   应用程序   Spring   POST   MediaType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!