Trước hết, trước khi bạn quyết định đóng câu hỏi của mình, tôi đã thử giải pháp this, nhưng nó không hoạt động đối với tôi.Không thể tạo đúng định dạng XML và JSON cùng một lúc
Tôi có dịch vụ REST được cho là trả lại JSON
hoặc XML
tùy thuộc vào tiêu đề Accept
. Tôi có thể có nó tạo ra JSON đúng, nhưng không phải là XML. Khi tôi sửa XML, JSON bị vặn. Dưới đây tôi trình bày mã của tôi.
XML có vẻ tốt, nhưng JSON không
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();
}
}
Đây là XML
kết quả, đó là 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>
Và đây là kết quả JSON
, chú ý đến các comments
. Tất cả những gì tôi cần là một mảng comments
.
[
{
"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."
}
]
}
}
]
Sửa JSON messes lên các phản ứng XML
Nếu tôi loại bỏ các @XmlElementWrapper
và @XmlElementRef
chú thích từ lớp Message
, sau đó nó hoạt động cho JSON, nhưng không 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
}
Các Comment
và MessageResource
lớp vẫn như cũ.
Dưới đây là kết quả tôi nhận được:
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>
Có ai biết làm thế nào để thực hiện hai công việc này lại với nhau? Giải pháp duy nhất mà tôi thấy là sử dụng JAXB cho XML và GSON cho JSON, nhưng tôi phải tự tạo các đối tượng JSON bằng cách sử dụng GSON.
Cảm ơn!
Các bạn đã nhìn vào tới [JacksonJaxbJSON] (http://fasterxml.github.io/jackson-jaxrs-json-provider/javadoc/2.0.1/com/ chú thích quickxml/jackson/jaxrs/json/JacksonJaxbJsonProvider.html)? như 'JsonType' .. – ulab
@ulab, bạn có thể chỉ cho tôi một ví dụ về cách sử dụng nó không? Tôi chưa từng thấy nó trước đây. –