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

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

How to consume XML data using Java and Spring Boot

问题

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

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

<TallyTransferResponse>
    <Response>
        <TransactionDocumentNo>iut-1</TransactionDocumentNo>
        <FromLocation>Bangalore</FromLocation>
        <ToLocation>Noida</ToLocation>
    </Response>
    <Response>
        <TransactionDocumentNo>iut-2</TransactionDocumentNo>
        <FromLocation>Bangalore</FromLocation>
        <ToLocation>Mumbai</ToLocation>
    </Response>
</TallyTransferResponse>

这是实体类的代码:

@Entity
public class TallyTransferResponse {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String transaction_document_no;
    private String from_location;
    private String to_location;

    public TallyTransferResponse() {}

    /**
     * @param transaction_document_no
     * @param from_location
     * @param to_location
     */
    public TallyTransferResponse(String transaction_document_no, String from_location, String to_location) {
        this.transaction_document_no = transaction_document_no;
        this.from_location = from_location;
        this.to_location = to_location;
    }
    // Getters and Setters
}

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

英文:

I need to consume XML data.
My XML Snippet:

&lt;TallyTransferResponse&gt;
    &lt;Response&gt;
        &lt;TransactionDocumentNo&gt;iut-1&lt;/TransactionDocumentNo&gt;
        &lt;FromLocation&gt;Bangalore&lt;/FromLocation&gt;
        &lt;ToLocation&gt;Noida&lt;/ToLocation&gt;
    &lt;/Response&gt;
    &lt;Response&gt;
        &lt;TransactionDocumentNo&gt;iut-2&lt;/TransactionDocumentNo&gt;
        &lt;FromLocation&gt;Bangalore&lt;/FromLocation&gt;
        &lt;ToLocation&gt;Mumbai&lt;/ToLocation&gt;
    &lt;/Response&gt;
&lt;/TallyTransferResponse&gt;

Here is code for entity class:

@Entity
public class TallyTransferResponse{
    @Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String transaction_document_no;
	private String from_location;
	private String to_location;
public TallyTransferResponse() {}
	/**
	 * @param transaction_document_no
	 * @param from_location
	 * @param to_location
	 */
	public TallyTransferResponse(String transaction_document_no, String from_location, String to_location) {
		this.transaction_document_no = transaction_document_no;
		this.from_location = from_location;
		this.to_location = to_location;
	}
//Getters and Setters
}

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

答案1

得分: 0

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

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

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

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

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

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

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

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:

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:

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:

@XmlRootElement(name=&quot;company&quot;, namespace=&quot;some.namespace&quot; )
@XmlAccessorType(XmlAccessType.NONE)
public class Company {
@XmlAttribute(name=&quot;id&quot;)
private Integer id;
@XmlElement(name=&quot;company_name&quot;)
private String companyName;
.....
//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:

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:

确定