在Java Rest API中使用json POST请求的问题

时间:2018-07-09 04:04:54

标签: java json rest jax-rs

我知道这个问题已经问过好几次了,但是即使做了所有这些回答后,我仍然很困惑。我是Java EE的新手,在使用Spring框架之前,我希望有一个坚实的基础。所以我的问题是我无法接受/处理发送到简单Java Rest API的json请求。我有一个调用Java rest API的前端网页。但是,即使通过“ PostMan”,我也会遇到相同的错误

  

“ HTTP状态415 –不支持的媒体类型”

我有一个带有两种方法的简单控制器类。接受String的Get方法工作正常。但是问题出在Post方法上。我的标头值为"content-type:application/json"。但问题仍然存在 我只想了解几件事。我有一个与json请求相同的字段名称的Java类。但是我的理解是@Consumes(MediaType.APPLICATION_JSON)注释足以让Jax RS解析json请求到java对象。还是我的班级需要其他注释?这是我的整个项目。 我也不确定我在pom.xml中定义的依赖项。

我们将不胜感激。预先感谢。

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>com.thomsoncodes</groupId>
<artifactId>demobank</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demobank</name>

<build>
    <finalName>demobank</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    -->
</dependencies>
<properties>
    <jersey.version>2.16</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

我的控制器类


@Path("/customerinfo")
public class CustomerInfoController {

  @POST
  @Path("/greeting")
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public String greetings(String message) {
      return "Hello " + message;
  }

  @POST
  @Path("/newcustomer")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response createNewCustomer(Customer customer) {        
      String result = "New customer is created : " + customer;
      return Response.status(201).entity(result).build();

  }


}

我的对象类


public class Customer {
   private String firstName;
   private String midName;
   private String lastName;
   private String citizenship;
   private String dob;
   private String ssn;
   private String city;
   private String state;
   private String country;
   private String email;
   private String phone;


   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getMidName() {
       return midName;
   }
   public void setMidName(String midName) {
       this.midName = midName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public String getCitizenship() {
       return citizenship;
   }
   public void setCitizenship(String citizenship) {
       this.citizenship = citizenship;
   }
   public String getDob() {
       return dob;
   }
   public void setDob(String dob) {
       this.dob = dob;
   }
   public String getSsn() {
       return ssn;
   }
   public void setSsn(String ssn) {
       this.ssn = ssn;
   }
   public String getCity() {
       return city;
   }
   public void setCity(String city) {
       this.city = city;
   }
   public String getState() {
       return state;
   }
   public void setState(String state) {
       this.state = state;
   }
   public String getCountry() {
       return country;
   }
   public void setCountry(String country) {
       this.country = country;
   }
   public String getEmail() {
       return email;
   }
   public void setEmail(String email) {
       this.email = email;
   }
   public String getPhone() {
       return phone;
   }
   public void setPhone(String phone) {
       this.phone = phone;
   }



 }

这是邮递员的请求正文


Method - POST
Header - content-type:application/json
Body - {
      "firstName" : "John",
      "midName" : "Null",
      "lastName" : "Doe",
      "citizenship" : "USA",
      "dob" : "MM/DD/YYYY",
      "ssn" : "1868138163",
      "city" : "City",
      "state" : "State","country" : "USA",
      "email":"email@example.com",
      "phone" : "1234567890"

    }

1 个答案:

答案 0 :(得分:2)

  

但是我的理解是@Consumes(MediaType.APPLICATION_JSON)注释足以让Jax RS解析对Java对象的json请求

不。那不是它的作用。如this post中所述,其目的是进行内容协商。

反序列化实际上是Entity provider(或MessageBodyReader / Wrider)。并且,如果找不到提供程序来处理转换,则会收到415错误。在您的情况下,您使用的JSON提供程序是MOXy,您可以在依赖项中看到它

<!-- uncomment this to get JSON support
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>
-->

MOXy的优点是它基于JAXB构建,因此,它需要使用相同的注释。使用JAXB,我们需要用@XmlRootElement注释模型。因此,如果将其添加到模型类中,它将可以正常工作。

如果您不想使用注释,可以使用Jackson代替MOXy,我仍然建议这样做。只需将上述artifactId更改为jersey-media-json-jackson