简体   繁体   中英

Spring MVC ResponseEntity is always empty when returning error in controller

I have the following controller:

@RequestMapping(value = "/api/activate", method = RequestMethod.POST)
public ResponseEntity testPost(@RequestBody ActivationRequest activationRequest) {
    try {
        // do stuff
        return ResponseEntity.status(HttpStatus.OK)
                .body(result.toString());
    } catch (ApiException e) {
        logger.error("error executing command", e);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(e.getMessage());
    }
}

When the response is HttpStatus.OK , I can read string response in the client:

22:34:34.416 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
22:34:34.417 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Thu, 11 Oct 2018 04:34:29 GMT[\r][\n]"
22:34:34.417 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: text/plain;charset=utf-8[\r][\n]"
22:34:34.417 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 11[\r][\n]"
22:34:34.417 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
22:34:34.417 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "OK[ont = 6]"
22:34:34.422 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
22:34:34.422 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 11 Oct 2018 04:34:29 GMT
22:34:34.422 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/plain;charset=utf-8
22:34:34.422 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 11

But when I return HttpStatus.BAD_REQUEST response is always empty:

22:35:54.307 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]"
22:35:54.308 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Thu, 11 Oct 2018 04:35:51 GMT[\r][\n]"
22:35:54.308 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 0[\r][\n]"
22:35:54.308 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
22:35:54.310 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 400 Bad Request
22:35:54.310 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 11 Oct 2018 04:35:51 GMT
22:35:54.311 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0

Even when e.getMessage() has a valid String with a message.

This is the code I use to consume this service:

private static void testPost(ActivationRequest result) {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpPost httpPost = new HttpPost("http://localhost:8080/api/activate");
        String json = new Gson().toJson(result);
        StringEntity entity;
        entity = new StringEntity(json);
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "application/json");
        CloseableHttpResponse response = client.execute(httpPost);
        String entityString = EntityUtils.toString(response.getEntity());
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(response.getStatusLine().getReasonPhrase() + ":" + entityString);
        } else {
            System.out.println(response.getStatusLine().getReasonPhrase() + ":" + entityString);
        }
    } catch (Exception e) {

    }
    //
}

我的错误.... getMessage()为null,因此为什么响应始终为空。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM