实际上我尝试在Spring中使用restTemplate调用get请求。对我的应用程序进行Debbuging清楚地表明已下载JSON,但自动映射不起作用。我的域对象列表仅包含0个值和空值。 当我从浏览器调用get请求时,我得到以下响应作为JSON(我在这里复制了3 192中的前两个记录):
[{"OrderId":77862,"DateAdded":"2016-04-30T02:25:40.263","OrderItemCorpusId":"HUBW","OrderItemCorpusOriginalId":null,"OrderItemCurrency":"HUF","OrderItemExchangeRate":1.00000,"OrderItemOriginalLocation":"HU","OrderItemBuyLocation":"HU","OrderItemPrice":1337.80314,"OrderItemDiscountId":0,"OrderItemDiscountValue":"","DocumentId":25140,"Title":"Romana Gold 10. kötet","PublisherOriginalName":"Harlequin Kiadó","ISBN":"9789634073369"},{"OrderId":77864,"DateAdded":"2016-04-30T15:49:22.61","OrderItemCorpusId":"HUBW","OrderItemCorpusOriginalId":null,"OrderItemCurrency":"HUF","OrderItemExchangeRate":1.00000,"OrderItemOriginalLocation":"HU","OrderItemBuyLocation":"HU","OrderItemPrice":2748.03149,"OrderItemDiscountId":0,"OrderItemDiscountValue":"","DocumentId":25252,"Title":"Az eltűnt lány","PublisherOriginalName":"Harlequin Kiadó","ISBN":"9789634072423"}]
我的POJO域对象应保留转换后的数据来自JSON:
@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
private long OrderId;
private Date DateAdded;
private String OrderItemCurrency;
private double OrderItemExchangeRate;
private String OrderItemBuyLocation;
private double OrderItemPrice;
private String OrderItemDiscountValue;
private long DocumentId;
private String Title;
private String PublisherOriginalName;
private String ISBN;
//getters and setters
最后我用于其余请求的代码片段:
String startDate = new SimpleDateFormat("yyyy-MM-dd").format(start.getTime());
String endDate = new SimpleDateFormat("yyyy-MM-dd").format(end.getTime());
UriComponents uri = UriComponentsBuilder.newInstance().scheme("http").host("www.bookandwalk.hu")
.path("/api/AdminTransactionList").queryParam("password", "XXX")
.queryParam("begindate", startDate).queryParam("enddate", endDate).queryParam("corpusid", "HUBW")
.build().encode();
LOG.log(Level.INFO, "{0} were called as a rest call", uri.toString());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set("User-Agent", "Anything");
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<List<BandWTransaction>> transResponse = restTemplate.exchange(uri.toString(), HttpMethod.GET,
entity, new ParameterizedTypeReference<List<BandWTransaction>>() {
});
List<BandWTransaction> transactions = transResponse.getBody();
当我调试应用程序时,我意识到事务列表包含带有null和0值的对象。更准确地说,列表中没有和objcet在属性中有其他值为0和null。 我还检查了springTemplate.messageConverters ArrayList 9 HttpMessageConverter中自动注册的spring boot。此ArrayList的第7个元素是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter,它支持application / json和application / + json媒体类型。 任何想法都可以解决这个问题,因为我是春季的新手和一般的JSON映射。
答案 0 :(得分:3)
由于您的字段变量以大写字母开头,因此您似乎遇到了命名约定问题。当Jackson找到一对getTitle/setTitle
时,该变量的名称为title
(以小写字母开头)。当然,如果更改变量的大小写,json属性和java变量具有不同的名称,因此映射仍然失败。最好的解决方案是更改您的名称以符合Java约定,并使用Jackson注释来定义映射。
@JsonProperty(value="OrderId")
private int orderId;
@JsonProperty(value="DateAdded")
private Date dateAdded;
希望它有所帮助。
答案 1 :(得分:1)
我可以建议你编写一个测试并检查quickxml ObjectMapper如何读取json和unmarshall json到你的对象:
ObjectMapper objectMapper = new ObjectMapper();
String somestring = objectMapper.readValue("somestring", String.class);
只需用你的类替换 String ,用你的json替换“somestring”。所以你检查它是否有问题。
并尝试使用@JsonPropery导致所有这些大写字母字段以看起来凌乱开始:
@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
@JsonProperty("OrderId")
private long OrderId;
[...]
有了这些东西,我读了正确的json。你可以从另一边进来删除忽略未知属性:
@JsonIgnoreProperties(ignoreUnknown = true)//删除它并运行测试 公共班BandWTransaction {
你得到:
(11个已知属性:“dateAdded”,“orderItemExchangeRate”, “documentId”,“orderItemPrice”,“orderId”,“orderItemBuyLocation”, “orderItemDiscountValue”,“orderItemCurrency”,“isbn”,“title”, “publisherOriginalName”])
变量命名存在问题,您可以使用@JsonProperty
修复它