Cannot create properly formatted XML and JSON at the same time

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() { } // getters and setters } 

Comment.java

 import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "comment") public class Comment { int id; String text; public Comment() { } //getters and setters } 

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() { } //getters and setters } 

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!

+7
json rest xml jaxb
source share
1 answer

My proposed solution uses JAXB for XML (like you). But for JSON, it uses Jackson-JAXRS (unlike you), as described in this. So, instead of using GSON, you will need to use Jackson-JAXRS (e.g. Maven ).

To get the desired XML and JSON output, you need to configure annotations of the List<Comment> comments property in your Message class.

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Message { int id; String text; @XmlElementWrapper(name="comments") @XmlElementRef @JsonUnwrapped List<Comment> comments; //getters and setters } 

In @XmlElementRef you get every Comment object written as a <comment> element. Finally, in @XmlElementWrapper(name="comments") you will get all this in the <comments> element.

XML output:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <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> 

By @JsonUnwrapped (imported from the com.fasterxml.jackson.annotation package) you get List<Comment> comments , written as a simple array of objects.

JSON Output:

 [ { "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." } ] } ] 
+5
source share

All Articles