gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:47:06 24 4
gpt4 key购买 nike

我有一个非常简单的 Spring 应用程序(不是 spring boot)。我已经实现了 GET 和 POST Controller 方法。 GET 方法工作正常。但是 POST 正在抛出 415 Unsupported MediaType。下面提供了重现的步骤

服务 Controller 。 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 以下内容了

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 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>

最佳答案

我找到了解决方案,我想在这里发帖,以便其他人受益。

首先我需要在我的类路径中包含 jackson,我在 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'

接下来,我必须更改扩展 WebMvcConfigurerAdapterAppConfig,如下所示:

@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);
}
}

这一切都很好,一切都很好

关于spring - 415 Spring 应用程序中的 POST 请求不支持 MediaType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38066591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com