Я следую учебному пособию с Pullasight.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;
}
}
КнигаРесурс:
@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();
}
}
Ответ от Вадла
<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>
Ответ на API /getBooks
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>.