SpringFramework HttpClientErrorException 400错误的请求,如何在Java端进行POST?

时间:2018-11-24 16:47:41

标签: java rest web-services spring-boot

我尝试将对象发布到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]

我的源代码:

@Entity

@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;
}
}

@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

1 个答案:

答案 0 :(得分:0)

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

let obj = {
  	music:{
    	"do you like music":"Yes I do",
      "what music do you like": "Jazz"
    },
    
    sports:{
    	"Do you like sports": "Yes"
    }
};

function run(i) {
  return i.split(' ').flatMap(d => obj[d] || [])[0]
}

console.log(run("i like music"))

console.log(run("i like sports"))

并在您的控制器上更新:

@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;
    }
}