在控制器中返回错误时,Spring MVC ResponseEntity始终为空

时间:2018-10-11 04:39:05

标签: java spring-mvc apache-httpclient-4.x

我有以下控制器:

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

当响应为 HttpStatus.OK 时,我可以在客户端中读取字符串响应:

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

但是当我返回 HttpStatus.BAD_REQUEST 时,响应始终为空:

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

即使 e.getMessage()具有带消息的有效字符串。

这是我用来使用此服务的代码:

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) {

    }
    //
}

1 个答案:

答案 0 :(得分:0)

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

相关问题