gpt4 book ai didi

java - SpringFramework HttpClientErrorException 400错误请求,如何在java端POST?

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

我尝试将对象发布到 https://api.aftership.com/v4但遇到了以下问题:

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at api.aftership.tracking.TrackingController.addTracking(TrackingController.java:70) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]

我的源代码:

@实体

@JsonIgnoreProperties(ignoreUnknown = true)
public class Trackings {
private String tracking_number;
public Trackings() {

}
public Trackings(String tracking_number) {
super();
this.tracking_number = tracking_number;
}

@HeaderConfig

public class HttpHeaderConfigs {
private static final HttpHeaders headers = new HttpHeaders();

public HttpHeaderConfigs() {

}

public static HttpHeaders HttpHeaderConfig() {
headers.set("aftership-api-key", "e7f28250-889d-48ce-80a1-52f4d16XXXX");
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return headers;
}

public HttpHeaders getHeaders() {
return headers;
}
}

@要发布的函数

@RequestMapping("/trackings/add")
public void addTracking() {
final String uri = "https://api.aftership.com/v4/trackings";
Trackings tracking = new Trackings();
tracking.setTracking_number("783518514479");
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Trackings> entity = new HttpEntity<Trackings>(tracking, HttpHeaderConfig());
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
System.out.println(response.getBody());
}

输入 http://localhost:8080/trackings/add 时得到的结果是:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Nov 24 23:20:59 ICT 2018
There was an unexpected error (type=Internal Server Error, status=500).
400 Bad Request

我试图解决这个问题。请帮我解决它! <3

最佳答案

[Aftership API][1] 您需要序列化 ​​Trackings JSON 对象并添加其 get 和 set 方法:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MainTracking implements Serializable {

private Tracking Tracking;

public String getTracking() {
return tracking;
}

public void setTracking(Tracking tracking) {
this.tracking = tracking;
}
}



@JsonIgnoreProperties(ignoreUnknown = true)
public class Tracking implements Serializable {

private String tracking_number;

public Tracking() {
}

public Tracking(String tracking_number) {
setTracking_number(tracking_number);
}

public String getTracking_number() {
return tracking_number;
}

public void setTracking_number(String tracking_number) {
this.tracking_number = tracking_number;
}
}

并更新您的 Controller :

@RequestMapping("/trackings/add")
public void addTracking() {
final String uri = "https://api.aftership.com/v4/trackings";
MainTracking mainTracking = new MainTracking();
Tracking tracking = new Tracking();
tracking.setTracking_number("7835112328"); // Be careful with this number has generated different every time.!!
mainTracking.setTracking(tracking);

RestTemplate restTemplate = new RestTemplate();
HttpEntity<MainTracking> entity = new HttpEntity<MainTracking>(mainTracking, HttpHeaderConfig());

ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
System.out.println(response.getBody());
}

关于java - SpringFramework HttpClientErrorException 400错误请求,如何在java端POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460316/

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