我正在尝试将 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"
}
]
我是一名优秀的程序员,十分优秀!