Java Grizzly在MediaType.Application_JSON上失败
下面是pluralsight.com关于如何设置grizzly服务器和创建RESTAPI的教程。 若我的方法@products plane text=那个么一切都正常,但若我有api方法返回一个json文件,我将得到Java Grizzly在MediaType.Application_JSON上失败,java,rest,maven,jersey-2.0,grizzly,Java,Rest,Maven,Jersey 2.0,Grizzly,下面是pluralsight.com关于如何设置grizzly服务器和创建RESTAPI的教程。 若我的方法@products 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
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>
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>
response on/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>.
来自BOOKS API的打印行
2014年10月31日上午11:11:58 org.glassfish.jersey.message.internal.writeringterceptorexecutor$terminalwriteringterceptor aroundWriteTo
严重:未找到媒体类型=应用程序/json、类型=类java.util.ArrayList、genericType=java.util.List的MessageBodyWriter。
的评论是正确的
您需要使用GenericEntity
:
表示泛型类型T的响应实体。
通常情况下,类型擦除会删除泛型类型信息,例如,包含List
类型的实体的响应实例在运行时似乎包含原始List
因此,您需要实现如下内容:
@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();
}
祝您有愉快的一天…Zyexal可能重复,因此我将@Get方法修改为。。但我还是有错误。它与前一个稍有不同,因为我以前使用reutrn hashMap,现在我使用getDrivers方法返回List对象,所以现在它说>>>>严重:未找到MessageBodyWriter for media type=application/json,type=class java.util.ArrayList,genericType=java.util.Collection。在我的情况下,通用性似乎并不重要。请问您有什么建议吗?请更新您的问题。添加修改后的代码和异常stacktrace-如果可能的话。这似乎是一个依赖问题。因此,请将maven pom.xml/lib文件夹内容添加到您的问题中。请再加上司机,谢谢。我认为我遗漏了一些东西,所以我根据教程中的所有说明/说明重新创建了项目,他们使用书籍而不是驱动程序。我将在一秒钟内更新所有类以及maven pom文件。非常感谢您的回复。希望这能有所帮助,因为afaig在您的项目中除了DEP之外没有其他问题:(Zyeaxal..我想我是个盲人..在这个maven项目创建的pom文件中,对json的依赖性被注释掉了。2天来尝试了不同的事情…没有线索检查pom文件。首先,我不明白你的意思,但我认为我在某处看到了..无论如何,再次感谢你帮助我解决它。非常感谢
<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>
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>.
@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();
}
<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>