Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用spring restTemplate将JSON转换为域对象_Json_Spring_Spring Boot - Fatal编程技术网

无法使用spring restTemplate将JSON转换为域对象

无法使用spring restTemplate将JSON转换为域对象,json,spring,spring-boot,Json,Spring,Spring Boot,实际上,我尝试在Spring中使用restTemplate调用get请求。对我的应用程序进行调试清楚地表明JSON已下载,但自动映射不起作用。我的域对象列表仅包含0个值和空值。 当我从浏览器调用get请求时,我得到以下JSON响应(我在这里复制了3 192条记录中的前两条记录): 我的POJO域对象,该对象应保存JSON转换后的数据: @JsonIgnoreProperties(ignoreUnknown = true) public class BandWTransaction { priva

实际上,我尝试在Spring中使用restTemplate调用get请求。对我的应用程序进行调试清楚地表明JSON已下载,但自动映射不起作用。我的域对象列表仅包含0个值和空值。 当我从浏览器调用get请求时,我得到以下JSON响应(我在这里复制了3 192条记录中的前两条记录):

我的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
最后,我用于rest get请求的代码片段:

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();
String startDate=新的SimpleDateFormat(“yyyy-MM-dd”).format(start.getTime());
字符串endDate=新的SimpleDateFormat(“yyyy-MM-dd”).format(end.getTime());
UriComponents uri=UriComponentsBuilder.newInstance().scheme(“http”).host(“www.bookandwalk.hu”)
.path(“/api/AdminTransactionList”).queryParam(“密码”,“XXX”)
.queryParam(“begindate”,startDate)。queryParam(“enddate”,enddate)。queryParam(“小体ID”,“HUBW”)
.build().encode();
LOG.LOG(Level.INFO,“{0}作为rest调用调用”,uri.toString());
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set(“用户代理”、“任何内容”);
HttpEntity=新的HttpEntity(标题);
ResponseEntity transResponse=restTemplate.exchange(uri.toString(),HttpMethod.GET,
实体,新的ParameteredTypeReference(){
});
列表事务=transResponse.getBody();
当我调试应用程序时,我意识到事务列表中包含了满是null和0值的对象。更准确地说,列表中没有和objset,属性中的其他值为0和null。 我还检查了spring引导是否自动注册到restTemplate.messageConverters ArrayList 9 HttpMessageConverter中。此ArrayList的第7个元素是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter,它支持application/json和application/+json媒体类型。
我对解决这个问题很感兴趣,因为我在spring和JSON映射方面都是新手。

我可以建议您编写一个测试,检查fasterxml ObjectMapper如何快速读取JSON并将JSON解组到您的对象:

ObjectMapper objectMapper = new ObjectMapper();
String somestring = objectMapper.readValue("somestring", String.class);
只需用类替换字符串,用json替换“somestring”。所以你要检查它是否有问题

并尝试使用@jsonproperty,因为所有这些大写字母字段的开头看起来都很混乱:

@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
    @JsonProperty("OrderId")
    private long OrderId;
    [...]
有了这些东西,我读的json是正确的。您可以从另一侧进入,忽略未知属性:

@JsonIgnoreProperties(ignoreUnknown=true)//删除它并运行测试 公营班次交易{

你会得到:

(11个已知属性:“dateAdded”、“orderItemExchangeRate”, “documentId”、“orderItemPrice”、“orderId”、“orderItemBuyLocation”, “orderItemDiscountValue”、“orderItemCurrency”、“isbn”、“title”, “发布者原始名称”])


因此,变量命名中存在问题,您可以使用@JsonProperty解决这个问题。我建议您编写一个测试,检查fasterxml ObjectMapper如何读取json并将json解组到您的对象:

ObjectMapper objectMapper = new ObjectMapper();
String somestring = objectMapper.readValue("somestring", String.class);
只要用类替换字符串,用json替换“somestring”,就可以检查它是否有问题

并尝试使用@jsonproperty,因为所有这些大写字母字段的开头看起来都很混乱:

@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
    @JsonProperty("OrderId")
    private long OrderId;
    [...]
有了这些东西,我读到的json是正确的。你可以从另一边进来,忽略未知属性:

@JsonIgnoreProperties(ignoreUnknown=true)//删除它并运行测试 公营班次交易{

你会得到:

(11个已知属性:“dateAdded”、“orderItemExchangeRate”, “documentId”、“orderItemPrice”、“orderId”、“orderItemBuyLocation”, “orderItemDiscountValue”、“orderItemCurrency”、“isbn”、“title”, “发布者原始名称”])


因此,变量命名中存在问题,您可以使用@JsonProperty解决这个问题,因为您的字段变量以大写开头,所以您似乎存在命名约定问题。当Jackson找到一对
getTitle/setTitle
时,会测量此变量的名称为
title
(以小写开头)。当然,如果更改变量的大小写,json属性和java变量的名称不同,因此映射仍然失败。最好的解决方案是更改名称以满足java约定,并使用Jackson注释定义映射

@JsonProperty(value="OrderId")
private int orderId;
@JsonProperty(value="DateAdded")
private Date dateAdded;

希望有帮助。

由于字段变量以大写开头,您似乎遇到了命名约定问题。当Jackson找到一对
getTitle/setTitle
时,会检测到此变量的名称是
title
(以小写开头)。当然,如果更改变量的大小写,json属性和java变量的名称不同,因此映射仍然失败。最好的解决方案是更改名称以满足java约定,并使用Jackson注释定义映射

@JsonProperty(value="OrderId")
private int orderId;
@JsonProperty(value="DateAdded")
private Date dateAdded;

希望有帮助。

A尝试了您的建议@Sergii。我使用了以下测试代码:“ObjectMapper om=new ObjectMapper();TypeReference ref=new TypeReference(){};try{transactions=om.readValue(“[…]”,ref);}catch(IOException e){//TODO自动生成的catch块e.printStackTrace();}'取消协议,它还为BandWTransaction域对象生成了0和0值的映射。没有异常。好的,我在本地运行它,并用@JsonProperty:
@JsonIgnoreProperties(ignoreUnknown=true)公共类BandWTransaction{@JsonProperty(“OrderId”)修复它。'private long OrderId;
可能是大写字母属性statrt的问题。但是当我直接编写json属性时,它工作得很好,我又学到了一些东西。我应用了不同的命名约定,因为REST API是用C#编写的,它们有其他命名约定。我不知道这个注释,尽管它非常符合逻辑在J的情况下为e