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块中。为什么?