gpt4 book ai didi

java - Spring 启动: Json Response is not Mapping to Entity

转载 作者:行者123 更新时间:2023-11-30 05:19:51 26 4
gpt4 key购买 nike

我正在尝试通过安全的 RESTful API 映射我使用的 Json 响应;但是,由于某种原因,映射没有正确发生,因为我不断收到 NULL 响应而不是填充的对象。

这是我的实体:

@JsonTypeName("order")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME)
public class Order {

@JsonProperty("id")
private long customerd;

@JsonProperty("email")
private String customeEmail;


public long getCustomerd() {
return customerd;
}

public void setCustomerd(long customerd) {
this.customerd = customerd;
}


public String getCustomeEmail() {
return customeEmail;
}

public void setCustomeEmail(String customeEmail) {
this.customeEmail = customeEmail;
}
}

这是我的服务方法:

   public Order orderDetails (@RequestBody Order 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/2013413015655.json";

RestTemplate restTemplate = new RestTemplate();

HttpEntity request = new HttpEntity(headers);

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

return order;
}

这是我的 Controller 方法:

    @GetMapping("/orderdetails")
@ResponseStatus(HttpStatus.FOUND)
public Order getBasicAut(Order order){
return basicAuth.orderDetails(order);
}

最佳答案

您应该配置 REST 以生成 JSON 内容

@GetMapping(path = "/orderdetails", produces = {MediaType.APPLICATION_JSON_VALUE})

具体来说,下面的代码运行良好

@RestController
public class JsonController {

@GetMapping(path = "/orderdetails", produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseStatus(HttpStatus.FOUND)
public Order sendJsonBack() {
final Order order = new Order();
order.setCustomerd(123L);
order.setCustomeEmail("mail@gmail.com");
return order;
}
}


@Data
class Order implements Serializable {

private static final long serialVersionUID = -4258392267221190600L;

@JsonProperty("id")
private long customerd;

@JsonProperty("email")
private String customeEmail;

}

关于java - Spring 启动: Json Response is not Mapping to Entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59741566/

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