First off, before you decide to close my question, I've tried this solution, but it's not working for me.
I have a REST service that is supposed to return JSON
or XML
depending on the Accept
header. I can have it generate proper JSON, but not XML. When I fix the XML the JSON gets screwed. Below I'm presenting my code.
XML seems good, but JSON 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 OK:
<messages>
<message>
<id>1</id>
<text>Java is an OOP language.</text>
<comments>
<comment>
<id>20</id>
<text>That's 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's correct.</text>
</comment>
<comment>
<id>30</id>
<text>test test.</text>
</comment>
</comments>
</message>
</messages>
And here's the JSON
result, pay attention to the comments
. All I need is a comments
array.
[
{
"id": 1,
"text": "Java is an OOP language.",
"comments": {
"comment": [
{
"id": 20,
"text": "That's correct."
},
{
"id": 30,
"text": "test test"
}
]
}
},
{
"id": 1,
"text": "Java is an OOP language.",
"comments": {
"comment": [
{
"id": 20,
"text": "That's correct."
},
{
"id": 30,
"text": "test test."
}
]
}
}
]
Fixing the JSON messes up the XML response
If I remove the @XmlElementWrapper
and @XmlElementRef
annotations 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
classes remain the same.
Here's the results I get:
JSON
- OK
[
{
"id": 1,
"text": "Java is an OOP language.",
"comments": [
{
"id": 20,
"text": "That's correct."
},
{
"id": 30,
"text": "test test"
}
]
},
{
"id": 1,
"text": "Java is an OOP language.",
"comments": [
{
"id": 20,
"text": "That's correct."
},
{
"id": 30,
"text": "test test."
}
]
}
]
XML
- WRONG
<messages>
<message>
<id>1</id>
<text>Java is an OOP language.</text>
<comments>
<id>20</id>
<text>That's 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's 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 to this is using JAXB for XML and GSON for JSON, but I have to manually create JSON objects using GSON.
Thanks!