2017-05-02 19 views
7

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@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 CommentMessageResource 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!

+0

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

+0

@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. –

Trả lời

5

Giải pháp được đề xuất của tôi sử dụng JAXB cho XML (như bạn). Nhưng đối với JSON, nó sử dụng Jackson-JAXRS (không giống như bạn), ví dụ như được mô tả trong số answer này. Vì vậy, thay vì sử dụng GSON, bạn sẽ cần phải sử dụng Jackson-JAXRS (ví dụ từ Maven).

Để có được đầu ra XML và JSON mong muốn, bạn cần điều chỉnh chú thích thuộc tính List<Comment> comments trong lớp Message của mình.

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Message { 
    int id; 
    String text; 

    @XmlElementWrapper(name="comments") 
    @XmlElementRef 
    @JsonUnwrapped 
    List<Comment> comments; 

    //getters and setters 
} 

By @XmlElementRef bạn nhận được mỗi Comment đối tượng viết như một yếu tố <comment>. Cuối cùng, bằng cách @XmlElementWrapper(name="comments") bạn nhận được tất cả các gói này trong một phần tử <comments>.

Kết quả XML là:

<?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'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> 

By @JsonUnwrapped (nhập khẩu từ gói com.fasterxml.jackson.annotation) bạn sẽ có được List<Comment> comments viết như một mảng đồng bằng của các đối tượng.

Sản lượng JSON là:

[ 
    { 
    "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." 
     } 
    ] 
    } 
] 
+0

Cảm ơn! Giải pháp của bạn là tạo ra XML đúng và gần như đúng JSON, ngoại trừ mảng đó là tên "bình luận" và không phải là "chú thích". Tôi đã phải thay đổi chú thích XML để làm cho nó hoạt động. Tôi đã sử dụng các chú thích này 'XMLElementWrapper (name =" comments ") @XMLElementRef @ JsonUnrwapped'. –

+0

@TurikMirash Cảm ơn phản hồi của bạn! Tôi đã cập nhật câu trả lời của mình với chú thích XML được cải tiến của bạn. –

Các vấn đề liên quan