我尝试将对象POST到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;
}
@标头配置
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;
}
}
@POST 功能
@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;
}
}
并更新您的控制器:
@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());
}