无法同时生成格式正确的XML和JSON

前端之家收集整理的这篇文章主要介绍了无法同时生成格式正确的XML和JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,在您决定关闭我的问题之前,我已经尝试了 this解决方案,但它对我不起作用.

我有一个REST服务,应该根据Accept标头返回JSON或XML.我可以让它生成适当的JSON,但不生成XML.当我修复XML时,JSON被搞砸了.下面我介绍我的代码.

XML似乎很好,但JSON没有

Message.java

  1. import java.util.List;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElementRef;
  6. import javax.xml.bind.annotation.XmlElementWrapper;
  7. import javax.xml.bind.annotation.XmlRootElement;
  8.  
  9. @XmlRootElement
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. public class Message {
  12. int id;
  13. String text;
  14.  
  15. @XmlElementWrapper
  16. @XmlElementRef
  17. List<Comment> comments;
  18.  
  19. public Message() {
  20.  
  21. }
  22. // getters and setters
  23. }

Comment.java

  1. import javax.xml.bind.annotation.XmlRootElement;
  2.  
  3. @XmlRootElement(name = "comment")
  4. public class Comment {
  5. int id;
  6. String text;
  7.  
  8. public Comment() {
  9.  
  10. }
  11. //getters and setters
  12. }

MessageResource.java

  1. import java.util.List;
  2.  
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. import javax.ws.rs.core.Response;
  8.  
  9.  
  10. @Path("messages")
  11. public class MessageResource {
  12.  
  13. DBUtils db = new DBUtils();
  14.  
  15. @GET
  16. @Produces(MediaType.APPLICATION_XML)
  17. public Response getXML() {
  18. List<Message> messages = db.getMessages();
  19. return Response.ok(messages.toArray(new Message[messages.size()]),MediaType.APPLICATION_XML).build();
  20. }
  21.  
  22. @GET
  23. @Produces(MediaType.APPLICATION_JSON)
  24. public Response getJSON() {
  25. List<Message> messages = db.getMessages();
  26. return Response.ok(messages.toArray(new Message[messages.size()]),MediaType.APPLICATION_JSON).build();
  27. }
  28. }

这是XML结果,可以:

  1. <messages>
  2. <message>
  3. <id>1</id>
  4. <text>Java is an OOP language.</text>
  5. <comments>
  6. <comment>
  7. <id>20</id>
  8. <text>That's correct.</text>
  9. </comment>
  10. <comment>
  11. <id>30</id>
  12. <text>test test</text>
  13. </comment>
  14. </comments>
  15. </message>
  16. <message>
  17. <id>1</id>
  18. <text>Java is an OOP language.</text>
  19. <comments>
  20. <comment>
  21. <id>20</id>
  22. <text>That's correct.</text>
  23. </comment>
  24. <comment>
  25. <id>30</id>
  26. <text>test test.</text>
  27. </comment>
  28. </comments>
  29. </message>
  30. </messages>

这是JSON的结果,注意评论.我只需要一个注释数组.

  1. [
  2. {
  3. "id": 1,"text": "Java is an OOP language.","comments": {
  4. "comment": [
  5. {
  6. "id": 20,"text": "That's correct."
  7. },{
  8. "id": 30,"text": "test test"
  9. }
  10. ]
  11. }
  12. },{
  13. "id": 1,"text": "test test."
  14. }
  15. ]
  16. }
  17. }
  18. ]

修复JSON会弄乱XML响应

如果我从Message类中删除@XmlElementWrapper和@XmlElementRef注释,那么它适用于JSON,但不适用于XML.

Message.jave

  1. import java.util.List;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6.  
  7. @XmlRootElement
  8. @XmlAccessorType(XmlAccessType.FIELD)
  9. public class Message {
  10. int id;
  11. String text;
  12.  
  13. List<Comment> comments;
  14.  
  15. public Message() {
  16.  
  17. }
  18. //getters and setters
  19. }

Comment和MessageResource类保持不变.

这是我得到的结果:

JSON – 好的

  1. [
  2. {
  3. "id": 1,"comments": [
  4. {
  5. "id": 20,"text": "That's correct."
  6. },{
  7. "id": 30,"text": "test test"
  8. }
  9. ]
  10. },"text": "test test."
  11. }
  12. ]
  13. }
  14. ]

XML – 错误

  1. <messages>
  2. <message>
  3. <id>1</id>
  4. <text>Java is an OOP language.</text>
  5. <comments>
  6. <id>20</id>
  7. <text>That's correct.</text>
  8. </comments>
  9. <comments>
  10. <id>30</id>
  11. <text>test test</text>
  12. </comments>
  13. </message>
  14. <message>
  15. <id>1</id>
  16. <text>Java is an OOP language.</text>
  17. <comments>
  18. <id>20</id>
  19. <text>That's correct.</text>
  20. </comments>
  21. <comments>
  22. <id>30</id>
  23. <text>test test.</text>
  24. </comments>
  25. </message>
  26. </messages>

有谁知道如何让这两个一起工作?我发现的唯一解决方案是使用JAXB for XML和GSON for JSON,但我必须使用GSON手动创建JSON对象.

谢谢!

我提出的解决方案使用JAXB for XML(和你一样).但对于JSON,它使用Jackson-JAXRS(与您不同),例如在此 answer中描述的.
因此,您需要使用Jackson-JAXRS(例如 Maven),而不是使用GSON.

要获得所需的XML和JSON输出,您需要调整注释
列表<评论>您的Message类中的comments属性.

  1. @XmlRootElement
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Message {
  4. int id;
  5. String text;
  6.  
  7. @XmlElementWrapper(name="comments")
  8. @XmlElementRef
  9. @JsonUnwrapped
  10. List<Comment> comments;
  11.  
  12. //getters and setters
  13. }

通过@XmlElementRef,您可以将每个Comment对象写为< comment>元件.最后,通过@XmlElementWrapper(name =“comments”),您可以将所有这些包含在< comments>中.元件.

XML输出是:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <messages>
  3. <message>
  4. <id>1</id>
  5. <text>Java is an OOP language.</text>
  6. <comments>
  7. <comment>
  8. <id>20</id>
  9. <text>That's correct.</text>
  10. </comment>
  11. <comment>
  12. <id>30</id>
  13. <text>test test.</text>
  14. </comment>
  15. </comments>
  16. </message>
  17. <message>
  18. <id>1</id>
  19. <text>Java is an OOP language.</text>
  20. <comments>
  21. <comment>
  22. <id>20</id>
  23. <text>That's correct.</text>
  24. </comment>
  25. <comment>
  26. <id>30</id>
  27. <text>test test.</text>
  28. </comment>
  29. </comments>
  30. </message>
  31. </messages>

@JsonUnwrapped(从包com.fasterxml.jackson.annotation导入),您将获得List< Comment>写为平面对象数组的注释.

JSON输出是:

  1. [
  2. {
  3. "id": 1,"text": "test test."
  4. }
  5. ]
  6. },"text": "test test."
  7. }
  8. ]
  9. }
  10. ]

猜你在找的XML相关文章