Jersey - JerseyTest: Error reading entity from input stream -
i have created test project in order learn jersey. have created following classes book.java
(model), bookdao.java
, bookresource.java
, far works fine. problem jerseytest trying run (bookresourcetest.java
). using jackson json serialization.
when run test error: error reading entity input stream
. console output:
------------------------------------------------------- t e s t s ------------------------------------------------------- running com.example.bookresourcetest Ιουν 13, 2015 5:25:23 ΜΜ org.glassfish.jersey.test.grizzly.grizzlytestcontainerfactory$grizzlytestcontainer <init> info: creating grizzlytestcontainer configured @ base uri http://localhost:9998/ Ιουν 13, 2015 5:25:24 ΜΜ org.glassfish.grizzly.http.server.networklistener start info: started listener bound [localhost:9998] Ιουν 13, 2015 5:25:24 ΜΜ org.glassfish.grizzly.http.server.httpserver start info: [httpserver] started. Ιουν 13, 2015 5:25:24 ΜΜ org.glassfish.jersey.filter.loggingfilter log info: 1 * sending client request on thread main 1 > http://localhost:9998/books 1 > accept: application/json Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.jersey.filter.loggingfilter log info: 2 * client response received on thread main 2 < 200 2 < content-length: 177 2 < content-type: application/json 2 < date: sat, 13 jun 2015 14:25:25 gmt [{"id":"2","title":"title2","author":"author2","isbn":"12345","published":1434205524749},{"id":"1","title":"title1","author":"author1","isbn":"45678","published":1434205524749}] Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.grizzly.http.server.networklistener shutdownnow info: stopped listener bound [localhost:9998] Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.jersey.test.grizzly.grizzlytestcontainerfactory$grizzlytestcontainer <init> info: creating grizzlytestcontainer configured @ base uri http://localhost:9998/ Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.grizzly.http.server.networklistener start info: started listener bound [localhost:9998] Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.grizzly.http.server.httpserver start info: [httpserver-1] started. Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.jersey.filter.loggingfilter log info: 1 * sending client request on thread main 1 > http://localhost:9998/books/1 1 > accept: application/json Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.jersey.filter.loggingfilter log info: 2 * client response received on thread main 2 < 200 2 < content-length: 87 2 < content-type: application/json 2 < date: sat, 13 jun 2015 14:25:25 gmt {"id":"1","title":"title1","author":"author1","isbn":"45678","published":1434205525362} Ιουν 13, 2015 5:25:25 ΜΜ org.glassfish.grizzly.http.server.networklistener shutdownnow info: stopped listener bound [localhost:9998] tests run: 2, failures: 0, errors: 2, skipped: 0, time elapsed: 2.393 sec <<< failure! results : tests in error: testgetbooks(com.example.bookresourcetest): error reading entity input stream. testgetbook(com.example.bookresourcetest): error reading entity input stream. tests run: 2, failures: 0, errors: 2, skipped: 0
what doing wrong?
here project files:
pom.xml
<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.example</groupid> <artifactid>simple-service</artifactid> <packaging>jar</packaging> <version>1.0-snapshot</version> <name>simple-service</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 json support: <dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-moxy</artifactid> </dependency> --> <dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>${jersey.version}</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupid>org.glassfish.jersey.test-framework.providers</groupid> <artifactid>jersey-test-framework-provider-grizzly2</artifactid> <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.example.main</mainclass> </configuration> </plugin> </plugins> </build> <properties> <jersey.version>2.18</jersey.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> </project>
book.java
public class book { private string id; private string title; private string author; private string isbn; private date published; public book(string id, string title, string author, string isbn, date published) { this.id = id; this.title = title; this.author = author; this.isbn = isbn; this.published = published; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getauthor() { return author; } public void setauthor(string author) { this.author = author; } public string getisbn() { return isbn; } public void setisbn(string isbn) { this.isbn = isbn; } public date getpublished() { return published; } public void setpublished(date published) { this.published = published; } }
bookdao.java
public class bookdao { private map<string, book> books; public bookdao() { books = new hashmap<>(); books.put("1", new book("1", "title1", "author1", "45678", new date())); books.put("2", new book("2", "title2", "author2", "12345", new date())); } public collection<book> getbooks() { return (books.values()); } public book getbook(string id) { return books.get(id); } }
bookresource.java
@path("/books") public class bookresource { bookdao dao = new bookdao(); @get @produces({"application/xml", "application/json"}) public collection<book> getbooks() { return dao.getbooks(); } @path("/{id}") @get @produces({"application/xml", "application/json"}) public book getbook(@pathparam("id") string id) { return dao.getbook(id); } }
bookresourcetest.java
public class bookresourcetest extends jerseytest { @override protected application configure() { enable(testproperties.log_traffic); enable(testproperties.dump_entity); return new resourceconfig().packages("com.example"); } @test public void testgetbook() { book response = target("books").path("1").request().get(book.class); assertnotnull(response); } @test public void testgetbooks() { collection<book> response = target("books").request().get(new generictype<collection<book>>() { }); assertequals(2, response.size()); } }
so seems reason book.java didn't have default constructor jackson unable create instance of it...
public class book { private string id; private string title; private string author; private string isbn; private date published; //must have default constructor public book() { } public book(string id, string title, string author, string isbn, date published) { this.id = id; this.title = title; this.author = author; this.isbn = isbn; this.published = published; } ... }
Comments
Post a Comment