First of all, before you decide to close my question, I tried this solution, but it does not work for me.
I have a REST service that should return JSON or XML depending on the Accept header. I can get it to generate the correct JSON, but not XML. When I fix the XML, JSON is screwed. Below I present my code.
XML seems good, but JSON is not
Message.java
import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Message { int id; String text; @XmlElementWrapper @XmlElementRef List<Comment> comments; public Message() { }
Comment.java
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "comment") public class Comment { int id; String text; public Comment() { }
MessageResource.java
import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("messages") public class MessageResource { DBUtils db = new DBUtils(); @GET @Produces(MediaType.APPLICATION_XML) public Response getXML() { List<Message> messages = db.getMessages(); return Response.ok(messages.toArray(new Message[messages.size()]), MediaType.APPLICATION_XML).build(); } @GET @Produces(MediaType.APPLICATION_JSON) public Response getJSON() { List<Message> messages = db.getMessages(); return Response.ok(messages.toArray(new Message[messages.size()]), MediaType.APPLICATION_JSON).build(); } }
Here's the XML result, which is fine:
<messages> <message> <id>1</id> <text>Java is an OOP language.</text> <comments> <comment> <id>20</id> <text>That correct.</text> </comment> <comment> <id>30</id> <text>test test</text> </comment> </comments> </message> <message> <id>1</id> <text>Java is an OOP language.</text> <comments> <comment> <id>20</id> <text>That correct.</text> </comment> <comment> <id>30</id> <text>test test.</text> </comment> </comments> </message> </messages>
And here is the result of JSON , pay attention to comments . All I need is an array of comments .
[ { "id": 1, "text": "Java is an OOP language.", "comments": { "comment": [ { "id": 20, "text": "That correct." }, { "id": 30, "text": "test test" } ] } }, { "id": 1, "text": "Java is an OOP language.", "comments": { "comment": [ { "id": 20, "text": "That correct." }, { "id": 30, "text": "test test." } ] } } ]
JSON commit messed up XML response
If I remove the @XmlElementWrapper and @XmlElementRef from the Message class, then it works for JSON, but not XML.
Message.jave
import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Message { int id; String text; List<Comment> comments; public Message() { }
The Comment and MessageResource remain the same.
Here are the results I get:
JSON - OK
[ { "id": 1, "text": "Java is an OOP language.", "comments": [ { "id": 20, "text": "That correct." }, { "id": 30, "text": "test test" } ] }, { "id": 1, "text": "Java is an OOP language.", "comments": [ { "id": 20, "text": "That correct." }, { "id": 30, "text": "test test." } ] } ]
XML - INCORRECT
<messages> <message> <id>1</id> <text>Java is an OOP language.</text> <comments> <id>20</id> <text>That correct.</text> </comments> <comments> <id>30</id> <text>test test</text> </comments> </message> <message> <id>1</id> <text>Java is an OOP language.</text> <comments> <id>20</id> <text>That correct.</text> </comments> <comments> <id>30</id> <text>test test.</text> </comments> </message> </messages>
Does anyone know how to make these two work together? The only solution I found was to use JAXB for XML and GSON for JSON, but I need to manually create JSON objects using GSON.
Thanks!