gpt4 book ai didi

java - Spring 启动: InvalidTypeIdException When Trying to Parse Json

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:47 24 4
gpt4 key购买 nike

我正在尝试将 POJO 映射到我收到的 JSON 响应;但是,我不断收到此错误:

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'order' as a subtype of `com.issbou.entity.OrdersList`: known type ids = [orders]

我从服务器获得的 Json 响应以及我尝试映射到我的 POJO 类的 Json 响应是:

{
"orders": [
{
"id": 2039665099999,
"email": "isx@gmail.com"
},
{
"id": 2039665066666,
"email": "yzy@gmail.com"
}
]
}

订单:代表单个订单的Java实体类:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Order {

@JsonProperty("id")
private long id;

@JsonProperty("email")
private String email;

}

OrderList:创建一系列单个订单的 Java 实体类:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Getter;
import lombok.Setter;


@JsonTypeName("orders")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME)
@Setter
@Getter
public class OrdersList {

private List<Order> singleOrder;


}

服务等级:

@Service
public class BasicAuth {

public ResponseEntity<OrdersList> orderDetails (@RequestBody OrdersList order){

String username = "username";
String password = "password";
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);

// request url
String url = "https://test.myshopify.com/admin/orders.json";

RestTemplate restTemplate = new RestTemplate();


HttpEntity request = new HttpEntity(headers);

ResponseEntity<OrdersList> response = restTemplate.exchange(
url, HttpMethod.GET, request, OrdersList.class);

return response;
}

}

休息 Controller :

@RestController
public class Controller {

@Autowired
BasicAuth basicAuth;

@GetMapping("/lok")
@ResponseStatus(HttpStatus.FOUND)
public ResponseEntity<OrdersList> getBasicAut(OrdersList ordersList){

return basicAuth.orderDetails(ordersList);
}

}

最佳答案

您的 OrdersList 类不包含列表。如果您修改 OrdersList,使其看起来像这样:

import java.util.Collections.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Getter;
import lombok.Setter;

@JsonTypeName("orders")
@Setter
@Getter
public class OrdersList {

private List<Order> ordersList;
}

应该可以。

我宁愿做的是接受 Controller 中的订单列表

@RestController
public class Controller {

@Autowired
BasicAuth basicAuth;

@GetMapping("/lok")
@ResponseStatus(HttpStatus.FOUND)
public ResponseEntity<List<Order>> getBasicAut(List<Order> ordersList){

return basicAuth.orderDetails(ordersList);
}
}

然后像这样转移订单:

[
{
"id": 2039665099999,
"email": "isx@gmail.com"
},
{
"id": 2039665066666,
"email": "yzy@gmail.com"
}
]

关于java - Spring 启动: InvalidTypeIdException When Trying to Parse Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59792705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com