2017-05-21 23 views
0

は私のコントローラである:Javaの春415サポートされていないメディアタイプここ

@RestController 
public class UserController { 

    @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 
    public User someName(@RequestBody User user){ 
     System.out.println(user.toString()); 
     return user; 
    } 

} 

Userクラス:

@Entity 
@Table(name = "user") 
public class User { 

    @Id @GeneratedValue 
    @Column(name = "id") 
    private Integer id; 

    private String name; 
    private String secondname; 
    private String email; 

    public User() { 
    } 

    public User(Integer id, String name, String secondname, String email) { 
     this.id = id; 
     this.name = name; 
     this.secondname = secondname; 
     this.email = email; 
    } 

    public User(String name, String secondname, String email) { 
     this.name = name; 
     this.secondname = secondname; 
     this.email = email; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    @Column(name = "name") 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Column(name = "secondname") 
    public String getSecondname() { 
     return secondname; 
    } 

    public void setSecondname(String secondname) { 
     this.secondname = secondname; 
    } 

    @Column(name = "email") 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Override 
    public String toString() { 
     return new StringBuilder().append(this.name).append(", ").append(this.secondname).append(", ") 
       .append(this.email).toString(); 
    } 
} 

そして、私のpom.xml:私は投稿したい

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>pl</groupId> 
    <artifactId>javalab</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>javalab Maven Webapp</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
     <java-version>1.7</java-version> 
    </properties> 

    <repositories> 
     <!-- Repository for ORACLE JDBC Driver --> 
     <repository> 
      <id>codelds</id> 
      <url>https://code.lds.org/nexus/content/groups/main-repo</url> 
     </repository> 
    </repositories> 

    <dependencies> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Servlet API --> 
     <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
      <scope>provided</scope> 
     </dependency> 

     <!-- Jstl for jsp page --> 
     <!-- http://mvnrepository.com/artifact/javax.servlet/jstl --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 


     <!-- JSP API --> 
     <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.2</version> 
      <scope>provided</scope> 
     </dependency> 

     <!-- Spring dependencies --> 
     <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 

     <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 

     <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 

     <!-- http://mvnrepository.com/artifact/org.springframework/spring-orm --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 

     <!-- Hibernate --> 
     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.8.Final</version> 
     </dependency> 

     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.8.Final</version> 
     </dependency> 


     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-c3p0</artifactId> 
      <version>4.3.8.Final</version> 
     </dependency> 


     <!-- MySQL JDBC driver --> 
     <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.34</version> 
     </dependency> 

     <!-- Oracle JDBC driver --> 
     <dependency> 
      <groupId>com.oracle</groupId> 
      <artifactId>ojdbc6</artifactId> 
      <version>11.2.0.3</version> 
     </dependency> 

     <!-- SQLServer JDBC driver (JTDS) --> 
     <!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> 
     <dependency> 
      <groupId>net.sourceforge.jtds</groupId> 
      <artifactId>jtds</artifactId> 
      <version>1.3.1</version> 
     </dependency> 

     <!-- Mail Start --> 
     <dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>mail</artifactId> 
      <version>1.4.7</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 
     <!-- Mail End --> 


      <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.8.7</version> 
     </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.dataformat</groupId> 
      <artifactId>jackson-dataformat-xml</artifactId> 
      <version>2.8.7</version> 
     </dependency> 


     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.8.7</version> 
     </dependency> 

    </dependencies> 
    <build> 
    <finalName>javalab</finalName> 
    <plugins> 
      <plugin> 
       <groupId>org.apache.tomcat.maven</groupId> 
       <artifactId>tomcat7-maven-plugin</artifactId> 
       <version>2.2</version> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

郵便配達員の場合:

{ 
    "name": "test", 
    "secondname": "test", 
    "email": "[email protected]" 
} 

私はエラーがあります:415 Unsupported Media type は、より正確には私が持っている:郵便配達でThe server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

は私がrawJSON (application/json)を選択してください。ヘッダーとして私は書いた:Content-Typeをキーとし、application/jsonを値とします。

誰かが私が間違っていることを知っていますか?

+0

[Http 415 JSONでサポートされていないメディアタイプエラー]の複製があります(http://stackoverflow.com/questions/22566433/http-415-unsupported-media-type-error-with-json) –

+0

春の設定? –

答えて

0

クロムの検査を使用して、郵便配達員のリクエストのHTTPヘッダーを見ることができます。content-Typeがcorrentかどうかを確認します。

0

これは春にはとても一般的です。エンティティをシリアライズ可能にします。

+0

まだ動作しません。 –

関連する問題