gpt4 book ai didi

java - Grizzly 在 MediaType.Application_JSON 上失败

转载 作者:行者123 更新时间:2023-11-30 08:19:41 25 4
gpt4 key购买 nike

我正在学习 pluralsight.com 上关于如何设置 grizzly 服务器和创建 rest api 的教程。如果我的方法@produces plane text = 那么一切正常,但是如果我有 api 方法返回一个 json 文件而不是我得到

  Pom.file >>

<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.plurasight</groupId>
<artifactId>async-rest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>async-rest</name>

<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-grizzly2-http</artifactId>
</dependency>
<!-- uncomment this to get JSON support:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<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>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.plurasight.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<jersey.version>2.7</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Java 类文件:

 BookDao



public class BookDao {
private HashMap<String, Book> books;

BookDao() {
books = new HashMap<String, Book>();

Book book1 = new Book();
book1.setId("1");
book1.setAuthor("Gino");
book1.setTitle("Gino's book1");
book1.setAspn("1234");
book1.setPublishedDate(new Date());


Book book2 = new Book();
book1.setId("2");
book1.setAuthor("Gino2");
book1.setTitle("Gino's book2");
book1.setAspn("2234");
book1.setPublishedDate(new Date());


Book book3 = new Book();
book1.setId("3");
book1.setAuthor("Gino");
book1.setTitle("Gino's book1");
book1.setAspn("3234");
book1.setPublishedDate(new Date());

books.put(book1.getId(), book1);
books.put(book2.getId(), book2);
books.put(book3.getId(), book3);

}

List<Book> getBooks() {
List<Book> list = new ArrayList<Book>(books.values());

return list;
}

}

图书资源:

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

BookDao dao = new BookDao();

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks() {
System.out.println("PRINTLINE FROM BOOKS API");
// GenericEntity<List<Book>> entity = new GenericEntity<List<Book>>( dao.getBooks() ){};
GenericEntity<List<Book>> entity = new GenericEntity<List<Book>>(dao.getBooks()) {
};

return Response.ok(entity).build();
}

}

主类:

public class Main {
public static final String BASE_URI = "http://localhost:8080/myapp/";

public static HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig().packages("com.plurasight");
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}

wadl的回应

<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.7 2014-03-12 18:11:31"/>
<doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/myapp/application.wadl?detail=true"/>
<grammars/>
<resources base="http://localhost:8080/myapp/">
<resource path="/books">
<method id="getBooks" name="GET">
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
<resource path="myresource">
<method id="getIt" name="GET">
<response>
<representation mediaType="text/plain"/>
</response>
</method>
</resource>
</resources>
</application>

响应/getBooks api

PRINTLINE FROM BOOKS API
Oct 31, 2014 11:11:58 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.plurasight.Book>.

最佳答案

Cássio Mazzochi Molin s评论是正确的!

您需要使用 GenericEntity :

Represents a response entity of a generic type T.
Normally type erasure removes generic type information such that a Response instance that contains, e.g., an entity of type List<String> appears to contain a raw List<?> at runtime.

因此你需要实现类似的东西:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getDrivers() {
System.out.println("printline from drivers api");
GenericEntity<List<Driver>> entity = new GenericEntity<List<Driver>>( dao.getDrivers() ){};
return Response.ok(entity).build();
}

编辑 (更新问题)

除了您的依赖项,我没有发现任何问题。请加 MessageBodyWriter/-Reader通过添加正确的依赖项来支持。

我在 build 中使用的资源(Tomcat 7)是:

    <dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>

祝你有美好的一天......

关于java - Grizzly 在 MediaType.Application_JSON 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26667375/

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