我想创建一个获取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>
解决方法:
因此,为了对实体实体使用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提供了围绕该工件的wrapper module,将其包装在自动发现的功能(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>