3

我想创建一个获取json数据并发送json数据的rest api。

我的书类:

@Entity
public class Book {

    @Id
    private String isbn;

    public Book() {
        
    }
......
}

我的书服务:

@Path("/books")
public class BookService {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response buyBook(Book book){
        String isbn = book.getIsbn();
        int number = book.getStock();
   
        return Response.status(Response.Status.OK).entity("OK").build();
    }
......
}

当我尝试使用 POSTMAN 发布此内容时:

POST /books HTTP/1.1    
Host: localhost:5000    
Content-Type: application/json    
Cache-Control: no-cache    
Postman-Token: 3099cd3a-184a-e442-270a-c118930df2b5    

{    
    "isbn" : "",    
    "title" : "",    
    "author" : "",    
    "stock" : "2"    
}        

我的其余 API 向我发送此响应:

HTTP ERROR 415
Problem accessing /books. Reason:
Unsupported Media Type

编辑:(使用 pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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>wholesalerservice</groupId>
  <version>1.0-SNAPSHOT</version>
  <artifactId>wholesalerservice</artifactId>
  <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.10.v20150310</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.2.10.v20150310</version>
    </dependency>
 
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.0.1</version>
      <scope>provided</scope>
    </dependency>
   
    <dependency>
        <groupId>org.hibernate.ogm</groupId>
        <artifactId>hibernate-ogm-mongodb</artifactId>
        <version>4.1.3.Final</version>
    </dependency>
 
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>eclipselink</artifactId>
      <version>2.5.2</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
      <type>jar</type>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals><goal>copy-dependencies</goal></goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <name>WholesalerService</name>
</project>
4

2 回答 2

2

所以为了对实体主体使用 JSON 到 POJO 的转换,我们需要有一个MessageBodyReader(有时称为提供者)来处理转换。对于 Java,Jackson 已经成为事实上的 JSON 库有一段时间了,他们提供了一个支持 JAX-RS 的模块,@Nico Müller指出了这一点

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.5.3</version>
</dependency>

这样做的问题是,如果我们不配置/注册它,发现它的唯一方法是通过类路径扫描,大多数 Jersey 应用程序不依赖它。相反,Jersey 提供了一个围绕这些工件的包装器模块,它将它包装在一个自动发现的功能(版本 2.9+)中,所以我们不需要配置它

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version> <!-- 2.17 latest as of now -->
</dependency>

如果您使用的是 2.9 之前的 Jersey,您始终可以在您的应用程序类中注册该功能

@ApplicationPath("/api")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("...");
        register(JacksonFeature.class);
    }
}

或者在 web.xml 中将该功能注册为提供程序类

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.glassfish.jersey.jackson.JacksonFeature
    </param-value>
</init-param>
于 2015-05-03T17:16:44.130 回答
1

您是否在项目中包含了 jackson-jaxrs-json-provider?

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.5.3</version>
</dependency>

一般来说:您的图书服务缺少您尝试发布的书名、作者和库存。

于 2015-05-02T17:38:31.653 回答