【发布时间】:2020-02-25 05:01:28
【问题描述】:
我正在使用 Spring FeignClient 访问一个 RESTful 端点,该端点返回一个 xml, 我想以 JSON 形式获取响应,而 JSON 又将映射到 POJO。
1) When I access the endpoint on the browser, I get response as below,
<ns3:Products xmlns:ns2="http://schemas.com/rest/core/v1" xmlns:ns3="http://schemas/prod/v1">
<ProductDetails>
<ProdId>1234</ProdId>
<ProdName>Some Text</ProdName>
</ProductDetails>
</ns3:Products>
2) @FeignClient(value = "productApi", url = "http://prodservice/resources/prod/v1")
public interface ProductApi {
@GetMapping(value="/products/{productId}", produces = "application/json")
ProductDetails getProductDetails(@PathVariable("productId") String productId)
// where, /products/{productId} refers the RESTful endpoint
// by mentioning, produces = "application/json", I believe the response xml would be converted to JSON Java POJO.
3) POJO
public class ProductDetails {
private String ProdId;
private String ProdName;
//...setters & getters
}
4) Service Layer
ProductDetails details = productApi.getProductDetails(productId);
在“详细信息”对象中,ProdId 和 ProdName 都为 null。 我在这里错过了什么吗?首先,是否可以获得 JSON 而不是 XML 的响应?
【问题讨论】:
-
您可以将xml转换为json。