gpt4 book ai didi

java - Jersey : JSON Unsupported Media Type

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:52 25 4
gpt4 key购买 nike

我想创建一个获取 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>

关于java - Jersey : JSON Unsupported Media Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com