415 Unsupported MediaType für die POST-Anforderung in spring-Anwendung
Habe ich eine sehr einfache Spring
Anwendung (NICHT spring boot). Ich habe implementiert eine GET-und POST-controller-Methoden. die GET
Methode funktioniert gut. Aber die POST
wirft 415 Unsupported MediaType
. Schritte zum reproduzieren sind verfügbar unter
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("/");
}
}
Der code ist hier:
git clone https://bitbucket.org/SpringDevSeattle/springrestcontroller.git
./gradlew clean build tomatrunwar
Diese hoch dreht embedded tomcat.
Jetzt können Sie locken die folgenden
curl -X GET -H "Content-Type: application/json" "http://localhost:8095/myApp/service/example/sample"
funktioniert
Aber
curl -X POST -H "Content-Type: application/json" '{
"id":1,
"name":"sai"
}' "http://localhost:8095/myApp/service/example/sample2"
Wirft 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>
InformationsquelleAutor brain storm | 2016-06-28
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich die Lösung gefunden und die will ich hier posten, damit es anderen zugute.
Erstens ich brauchen, um jackson in meinen Klassenpfad, die ich Hinzugefügt, build.gradle wie folgt:
Nächsten, ich meine
AppConfig
erstrecktWebMvcConfigurerAdapter
wie folgt:Ist, dass alle und alles funktionierte schön
InformationsquelleAutor brain storm
Accept-Header könnte das Problem sein.
Soweit ich mich erinnere, wenn Sie senden Sie eine Anfrage via curl es fügt eine Standard-header -
accept : */*
Aber im Fall von JSON Sie haben zu schweigen von den accept-header
als
accept : application/json
dementsprechend haben Sie erwähnt, der content-Type.
Und etwas mehr, ich weiß nicht, was das ist, aber nicht Sie denken, Sie haben zu Ort "Anfrage mappings" wie die
Kann dies nicht der Fall sein, aber accept-header ist die Sache, denke ich ist das Hauptproblem.
Lösung 2
Seit Sie diesen code
Habe ich bereits konfrontiert dieses problem vor und die Lösung zwei kann hier helfen
FormHttpMessageConverter, die verwendet wird, für @RequestBody-annotierten Parameter bei der content-type ist application/x-www-form-urlencoded binden können Ziel-Klassen wie @ModelAttribute können). Daher müssen Sie die @ModelAttribute statt @RequestBody
Entweder Mit @ModelAttribute annotation statt @RequestBody wie diese
Sofern ich die gleiche Antwort an jemanden, und es half.
hier ist, dass Antwort von mir
Hey ! ich aktualisiert meine Antwort, ich glaube das ist das Problem. Bitte überprüfen Sie sloution 2 @brainstorm
hey ich habe@ModelAttribute annotation.Die Kontrolle ist auf die Methode, sondern mein Objekt-Daten ist die Anzeige null...?? jede Anregung
überprüfen Sie den Namen der Variablen, der Objekt-und der name der Felder im html-Code. Sie sollten identisch sein. Wie wenn es die variable String myName; dann in html, input Feld haben soll-Attribut name="myName"
InformationsquelleAutor Tahir Hussain Mir
können Sie versuchen, mithilfe der option-d in curl
Auch, wenn Sie windows benutzen, sollten Sie die escape-Anführungszeichen
InformationsquelleAutor Periklis Douvitsas