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;
> }
>
>
> }
>