代码之家  ›  专栏  ›  技术社区  ›  Mariano L

当控制器返回错误时,Spring MVC ResponseEntity总是空的

  •  0
  • Mariano L  · 技术社区  · 6 年前

    @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\u请求 响应总是空的:

    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、 获取消息() 包含消息的有效字符串。

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

    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 回复  |  直到 6 年前
        1
  •  0
  •   Mariano L    6 年前

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