英文:
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:
<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>
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<String> 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<Company> 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="company", namespace="some.namespace" )
@XmlAccessorType(XmlAccessType.NONE)
public class Company {
@XmlAttribute(name="id")
private Integer id;
@XmlElement(name="company_name")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论