Grizzly在MediaType.Application_JSON上失败

时间:2014-10-31 04:06:03

标签: java rest maven jersey-2.0 grizzly

我按照pluralsight.com上的教程了解如何设置灰熊服务器并创建休息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;
    }

}

BookResource:

@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>.

1 个答案:

答案 0 :(得分:3)

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

您需要使用GenericEntity

  

表示泛型类型T的响应实体   通常,类型擦除会删除泛型类型信息,以便包含例如类型List<String>的实体的响应实例在运行时似乎包含原始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();
}

修改 (更新后的问题)

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

我在构建中使用的资源(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>

祝你有愉快的一天......