Hi Brendan,
You need to add jersey-json module and its dependencies to your classpath.
See [1] for more details.
~Jakub
[1]
http://jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1817
On 3.11.2011 7:40, Brendan cheng wrote:
> Hi Jakub,
> I found log the file from server but not sure how to interpret it. Does it mean that I miss some of the providers for JSON? If so, how to fix it?Thanks for your time!the error message is==============
> Oct 29, 2011 4:22:03 PM com.sun.jersey.spi.container.ContainerResponse writeSEVERE: A message body writer for Java class hk.itags.server.unmanaged.Todo, and Java type class hk.itags.server.unmanaged.Todo, and MIME media type application/json was not foundOct 29, 2011 4:22:03 PM com.sun.jersey.spi.container.ContainerResponse writeSEVERE: The registered message body writers compatible with the MIME media type are:*/* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter com.sun.jersey.server.impl.template.ViewableMessageBodyWriter com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
> Oct 29, 2011 4:22:03 PM com.sun.jersey.spi.container.ContainerResponse traceExceptionSEVERE: Mapped exception to response: 500 (Internal Server Error)javax.ws.rs.WebApplicationException at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:273) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1140) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1053) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1043) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:406) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:477) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
> ----------------------------------------
>> Date: Tue, 1 Nov 2011 10:41:57 +0100
>> From: jakub.podlesak_at_oracle.com
>> To: users_at_jersey.java.net
>> Subject: [Jersey] Re: Application/json for JAXB class failed in 1.10
>>
>> Hi Brendan,
>>
>> As Santiago pointed out, it is hard to tell what could be wrong without
>> some more information, such as server side log messages (likely
>> including stack trace).
>>
>> You are talking specifically about 1.10 (probably a snapshot?) version.
>> Does the same application work for you when using 1.9.1?
>> Do you have the jersey-json module and it's dependencies on the
>> classpath (see [1])?
>>
>> You might also want to download example [2] and give it a try, the example
>> should be configured properly to with respect to the JAXB->JSON mapping
>>
>> ~Jakub
>>
>> P.S. I have noticed you do not take full advantage of the JAXB->XML/JSON
>> mapping.
>> I mean, you do not need separate resource methods for the two media types,
>> all you need is:
>>
>> @GET @Produces({"application/json", "application/xml")
>> public Todo getTodo() {
>> return yourTodo;
>> }
>>
>> P.P.S. You can maybe consider using the Jackson based POJO->JSON mapping
>> feature,
>> where you are not limited to JAXB when producing JSON, see [3] for details.
>>
>> [1]http://jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1817
>> [2]http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/json-from-jaxb/1.10-b05/json-from-jaxb-1.10-b05-project.zip
>> [3]http://jersey.java.net/nonav/documentation/latest/json.html#d4e903
>>
>> On 26.10.2011 12:50, Brendan cheng wrote:
>>> Hi,
>>> I'm learning JAXB XmlRootElement for a simple POJO object.
>>> I did successfully run
>>> System.out.println(service.path("examples").path("unmanaged")
>>> .path("todo").accept(MediaType.APPLICATION_XML)
>>> .get(String.class));
>>> but not
>>> System.out.println(service.path("examples").path("unmanaged") .path("todo").accept(MediaType.APPLICATION_JSON) .get(Todo.class));
>>> and the error is.
>>> Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GEThttp://192.168.56.101:7474/examples/unmanaged/todo returned a response status of 500 Internal Server Error
>>> at com.sun.jersey.api.client.WebResource.handle(WebResource.java:676)
>>> at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
>>> at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)
>>> at hk.itags.server.unmanaged.Test.main(Test.java:40)
>>> I copied the examples from a web site which I put here:
>>> package hk.itags.server.unmanaged;
>>>
>>>
>>> import javax.xml.bind.annotation.XmlRootElement;
>>>
>>>
>>> @XmlRootElement
>>> // JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON
>>> // Isn't that cool?
>>> public class Todo {
>>> private String summary;
>>> private String description;
>>> private int number;
>>> public String getSummary() {
>>> return summary;
>>> }
>>> public void setSummary(String summary) {
>>> this.summary = summary;
>>> }
>>> public void setSummary(int number) {
>>> this.number = number;
>>> }
>>> public int getNumber() {
>>> return number;
>>> }
>>> public String getDescription() {
>>> return description;
>>> }
>>> public void setDescription(String description) {
>>> this.description = description;
>>> }
>>> }
>>> and the resource
>>> package hk.itags.server.unmanaged;
>>>
>>>
>>> import javax.ws.rs.GET;
>>> import javax.ws.rs.Path;
>>> import javax.ws.rs.Produces;
>>> import javax.ws.rs.core.MediaType;
>>>
>>>
>>> @Path("/todo")
>>> public class TodoResource {
>>> // This method is called if XMLis request
>>> @GET
>>> @Produces( { MediaType.APPLICATION_XML })
>>> public Todo getXML() {
>>> Todo todo = new Todo();
>>> todo.setSummary("This is my 1 todo");
>>> todo.setDescription("This is my 1 todo");
>>> todo.setSummary(345);
>>> return todo;
>>> }
>>>
>>> @GET
>>> @Produces( { MediaType.APPLICATION_JSON})
>>> public Todo getJSON() {
>>> Todo todo = new Todo();
>>> todo.setSummary("This is my third todo");
>>> todo.setDescription("This is my third todo");
>>> todo.setSummary(347);
>>> return todo;
>>> }
>>>
>>> // This can be used to test the integration with the browser
>>> @GET
>>> @Produces( { MediaType.TEXT_XML })
>>> public Todo getHTML() {
>>> Todo todo = new Todo();
>>> todo.setSummary("This is my 2 todo");
>>> todo.setDescription("This is my 2 todo");
>>> todo.setSummary(346);
>>> return todo;
>>> }
>>>
>>>
>>> }
>>>
>