如何使用Java和Spring Boot消费XML数据

huangapple go评论103阅读模式
英文:

How to consume XML data using Java and Spring Boot

问题

以下是您提供的内容的翻译部分:

我需要消费 XML 数据。
我的 XML 片段:

  1. <TallyTransferResponse>
  2. <Response>
  3. <TransactionDocumentNo>iut-1</TransactionDocumentNo>
  4. <FromLocation>Bangalore</FromLocation>
  5. <ToLocation>Noida</ToLocation>
  6. </Response>
  7. <Response>
  8. <TransactionDocumentNo>iut-2</TransactionDocumentNo>
  9. <FromLocation>Bangalore</FromLocation>
  10. <ToLocation>Mumbai</ToLocation>
  11. </Response>
  12. </TallyTransferResponse>

这是实体类的代码:

  1. @Entity
  2. public class TallyTransferResponse {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private int id;
  6. private String transaction_document_no;
  7. private String from_location;
  8. private String to_location;
  9. public TallyTransferResponse() {}
  10. /**
  11. * @param transaction_document_no
  12. * @param from_location
  13. * @param to_location
  14. */
  15. public TallyTransferResponse(String transaction_document_no, String from_location, String to_location) {
  16. this.transaction_document_no = transaction_document_no;
  17. this.from_location = from_location;
  18. this.to_location = to_location;
  19. }
  20. // Getters and Setters
  21. }

我在如何编写用于消费此 XML 的服务和控制器方面遇到了困难。

英文:

I need to consume XML data.
My XML Snippet:

  1. &lt;TallyTransferResponse&gt;
  2. &lt;Response&gt;
  3. &lt;TransactionDocumentNo&gt;iut-1&lt;/TransactionDocumentNo&gt;
  4. &lt;FromLocation&gt;Bangalore&lt;/FromLocation&gt;
  5. &lt;ToLocation&gt;Noida&lt;/ToLocation&gt;
  6. &lt;/Response&gt;
  7. &lt;Response&gt;
  8. &lt;TransactionDocumentNo&gt;iut-2&lt;/TransactionDocumentNo&gt;
  9. &lt;FromLocation&gt;Bangalore&lt;/FromLocation&gt;
  10. &lt;ToLocation&gt;Mumbai&lt;/ToLocation&gt;
  11. &lt;/Response&gt;
  12. &lt;/TallyTransferResponse&gt;

Here is code for entity class:

  1. @Entity
  2. public class TallyTransferResponse{
  3. @Id
  4. @GeneratedValue(strategy=GenerationType.IDENTITY)
  5. private int id;
  6. private String transaction_document_no;
  7. private String from_location;
  8. private String to_location;
  9. public TallyTransferResponse() {}
  10. /**
  11. * @param transaction_document_no
  12. * @param from_location
  13. * @param to_location
  14. */
  15. public TallyTransferResponse(String transaction_document_no, String from_location, String to_location) {
  16. this.transaction_document_no = transaction_document_no;
  17. this.from_location = from_location;
  18. this.to_location = to_location;
  19. }
  20. //Getters and Setters
  21. }

I'm stuck as to how to write service and controller to consume this XML.

答案1

得分: 0

你可以使用 Spring 的 restTemplate 发起 (get/post) 请求到终端,并将响应作为字符串获取,例如:

  1. final ResponseEntity<String> response = restTemplate.getForEntity(endpointUrl, String.class);

然后将其映射到 XML 或 JSON。更直接的方式是在响应中请求并期望一个数据模型,就像这样:

  1. final ResponseEntity<Company> response = restTemplate.getForEntity(endpointUrl, Company.class);

在这种情况下,您需要向模型类添加一些 XML 绑定注释,例如:

  1. @XmlRootElement(name="company", namespace="some.namespace")
  2. @XmlAccessorType(XmlAccessType.NONE)
  3. public class Company {
  4. @XmlAttribute(name="id")
  5. private Integer id;
  6. @XmlElement(name="company_name")
  7. private String companyName;
  8. // 其余部分省略
  9. }

您可以通过在请求中添加一个额外的头部来从服务端点请求 JSON 响应,例如:

  1. Accept: application/json

然后数据模型类可以省略所有的 XML 绑定注释。

英文:

You can use spring's restTemplate to make a (get/post) request to the endpoint and fetch the response as a string like:

  1. final ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(endpointUrl, String.class);

And the map it to XML or JOSN.
More straight forward version of this might be requesting and expecting a data model in the response, like:

  1. final ResponseEntity&lt;Company&gt; response = restTemplate.getForEntity(endpointUrl, Company.class);

In this case you will have to add some XML bind annotation to your model class, like:

  1. @XmlRootElement(name=&quot;company&quot;, namespace=&quot;some.namespace&quot; )
  2. @XmlAccessorType(XmlAccessType.NONE)
  3. public class Company {
  4. @XmlAttribute(name=&quot;id&quot;)
  5. private Integer id;
  6. @XmlElement(name=&quot;company_name&quot;)
  7. private String companyName;
  8. .....
  9. //the rest of the class is omitted

You can request a JSON response from the service endpoint with adding one additional header to the request, like:

  1. Accept: application/json

Then the data model class cant omit all the XML binding annotations.

huangapple
  • 本文由 发表于 2020年9月17日 15:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定