在 Spring Boot 中处理 REST 服务
Handle REST Service in SpringBoot
我使用以下代码 POST
JSON 反对以下 URL
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
我会在 catch 块中得到 404
,看起来不错。但是当我换成另一个不存在的url时,例如
http://123.433.234.12/receive
它在 try 块中。为什么 ????
关于 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();
同样,您可以获得 HttpServerErrorException
.
的错误代码
据我所知 RestTemplate.exchange
方法抛出 RestClientException
。您的 catch 子句中有 HttpStatusCodeException
,它只是 RestClientException
子类之一。
您尝试访问的地址 (http://123.433.234.12/receive
) 不是有效地址,因此您无法从中获得任何响应(没有 200s,也没有 500s 或 400s)。尝试捕获 RestClientException
并打印其消息以查看发生了什么。然后你可以写一些代码来管理这种情况。
此外,如果这不起作用,请尝试逐步检查 ResponseEntity
是否为 null 以及它的内容。当我试图理解某种方法时,这就是我正在做的事情 ;=)
我使用以下代码 POST
JSON 反对以下 URL
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
我会在 catch 块中得到 404
,看起来不错。但是当我换成另一个不存在的url时,例如
http://123.433.234.12/receive
它在 try 块中。为什么 ????
关于 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();
同样,您可以获得 HttpServerErrorException
.
据我所知 RestTemplate.exchange
方法抛出 RestClientException
。您的 catch 子句中有 HttpStatusCodeException
,它只是 RestClientException
子类之一。
您尝试访问的地址 (http://123.433.234.12/receive
) 不是有效地址,因此您无法从中获得任何响应(没有 200s,也没有 500s 或 400s)。尝试捕获 RestClientException
并打印其消息以查看发生了什么。然后你可以写一些代码来管理这种情况。
此外,如果这不起作用,请尝试逐步检查 ResponseEntity
是否为 null 以及它的内容。当我试图理解某种方法时,这就是我正在做的事情 ;=)