代码之家  ›  专栏  ›  技术社区  ›  John Joe Mayank Nema

在SpringBoot中处理REST服务

  •  -3
  • John Joe Mayank Nema  · 技术社区  · 7 年前

    POST

            HttpEntity messageEntity = new HttpEntity(message, buildHttpHeaders(getTerminalId()));
            String theUrl = "http://123.433.234.12/receive";
            try {
                   System.out.println("In try block");
                   ResponseEntity<Dto> responseEntity= restTemplate.exchange(theUrl, HttpMethod.POST, messageEntity, Dto.class);
              } catch (HttpStatusCodeException ex) {
                  // get http status code
              }
    

    如果URL无效或服务不可用,我希望它抛出404或503之类的错误状态代码。不幸的是,它总是在 try 块有没有办法解决这个问题?

    输出

    In try block
    

     String theUrl = "http://123.433.234.12/receive" + transactionId; //invalid Id
       try {
              System.out.println("=========start=========");
              ResponseEntity<Dto> responseEntity= restTemplate.exchange(theUrl, HttpMethod.POST, messageEntity, Dto.class);
              System.out.println("=========end=========");
           } catch (HttpStatusCodeException ex) {
              String a = ex.getStatusCode().toString();
              System.out.println(a);
        }
    

    =========start=========
    2017-09-22 14:54:54 [xles-server-ThreadPool.PooledThread-0-running] ERROR c.r.abc.jpos.JposRequestListener - Error HttpStatusCode 500org.springframework.web.client.HttpServerErrorException: 500 null
    

    它停止并且不显示 ========end ======== 或中的任何状态代码 catch

    有效url

       http://abc0/receive/hello
    

     http://abc0/recei/hello
    

    我会得到 404 在catch block中,它看起来很好。但是当我切换到另一个不存在的url时,例如

     http://123.433.234.12/receive
    

    它在try块中。为什么?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Yogesh Badke    7 年前

    this doc ,你应该抓住 RestClientException HttpStatusCodeException .

    如果您想在特定场景中引发异常,可以这样做

        try {
            restTemplate.exchange(...);
        }
        catch (RestClientException e) {
            // implies error is related to i/o. 
            if (e instanceof ResourceAccessException) {
                // java.net.ConnectException will be wrapped in e with message "Connection timed out".
                if (e.contains(ConnectException.class)) {
                    // handle connection timeout excp
                }
            } else if (e instanceof HttpClientErrorException) {
                // Handle all HTTP 4xx error codes here;
    
            } else if (e instanceof HttpServerErrorException) {
                // Handle all HTTP 5xx error codes here
            }
        }
    

    对于 HttpClientErrorException

    HttpClientErrorException clientExcp = (HttpClientErrorException) e;
    HttpStatus statusCode = clientExcp.getStatusCode();
    

    像wise一样,您可以获得 HttpServerErrorException .

        2
  •  1
  •   Mateusz Kasperczyk    7 年前

    据我记忆所及 RestTemplate.exchange RestClientException HttpStatusCodeException RestClientException http://123.433.234.12/receive )不是有效地址,因此您无法从中获得任何响应(没有200s,但也没有500s或400s)。试着抓住 RestClientException 然后打印信息,看看发生了什么。然后您可以编写一些代码来管理这种情况。

    ResponseEntity 是空的,它在它的身体里有什么。这就是我在尝试理解某些方法时所做的;=)