- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过向第 3 方进行 RESTFul 调用(Spring RestTemplate)来处理一些请求。在代码中,我试图处理以下情况。
catch (final Exception ex) {
if (ex instanceof HttpClientErrorException) {
HttpClientErrorException hcee = (HttpClientErrorException)ex;
if(hcee.getStatusCode() == NOT_FOUND) {
throw new MyRecordNotFoundException(hcee);
}
}else {
handleRestClientException(ex, Constants.MYAPP);
}
这里是handleRestClientException实现
protected Exception handleRestClientException(Exception ex, String serviceName) throws Exception{
if (ex instanceof RestClientResponseException) {
RestClientResponseException rcre = (RestClientResponseException) ex;
throw new RestClientResponseException(serviceName, rcre.getRawStatusCode(),
rcre.getStatusText(), rcre.getResponseHeaders(), rcre.getResponseBodyAsByteArray(), null);
} else {
throw new Exception(serviceName, ex);
}
但是所有 org.springframework.web.client.RestTemplate.getForObject(String url, Class responseType, Map urlVariables) 都会抛出 RestClientException
这是 HttpClientErrorException 的父级
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.springframework.core.NestedRuntimeException
org.springframework.web.client.RestClientException
org.springframework.web.client.RestClientResponseException
org.springframework.web.client.HttpStatusCodeException
org.springframework.web.client.HttpClientErrorException
因此,我的代码中提到的 if 条件在处理时永远不会达到。
你能帮我有效地处理这个层次结构中的每个异常吗?
最佳答案
您永远不应该在 catch block 中执行 if-else
来处理不同的异常。该代码不可读,执行速度可能较慢,并且在您的示例中,任何异常(HttpClientErrorException
除外)都像 RestClientException
一样处理。
使用适当的 catch block 来处理它们,如下所示(首先是更具体的异常,即 RestClientException
之前的 HttpClientErrorException
:
catch (HttpClientErrorException hcee) {
if (hcee.getStatusCode() == NOT_FOUND) {
throw new MyRecordNotFoundException(hcee);
}
}
catch (final RestClientException rce) {
handleRestClientException(rce, Constants.MYAPP);
}
关于java - 处理 RestClientException 和 HttpClientErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55947798/
我使用 httpResp.sendError(400, "You are missing customer id") 发回响应 当我尝试在客户端检索消息时(使用 Rest 模板调用端点)。 但是,打印
我有一个返回 HttpClientErrorException.NotFound 的第三方服务。当它返回此异常时,我从应用程序中抛出异常,表示输入无效,但对于所有其他异常(例如服务不可用等),我需要使
我正在更新使用 Spring 3.1 Framework 的 RestTemplate 类的 exchange 方法的旧代码。我发现似乎有一个重大的细节遗漏。当我尝试与之通信的其余客户端返回 400
我在从第三方服务器的 403 http 代码响应获取响应正文时遇到问题。在 2019 年 4 月 5 日之前,我能够正确从第三方获取响应正文,但现在我正在检索一个可能无法从 getResponseBo
我正在尝试从“https://swapi.co/api/people/?format=json ”获取数据并使用 RestTemplate.getForObject() 方法将其放入 POJO 中。我
我通过向第 3 方进行 RESTFul 调用(Spring RestTemplate)来处理一些请求。在代码中,我试图处理以下情况。 catch (final Exception ex) {
我尝试将对象发布到 https://api.aftership.com/v4但遇到了以下问题: org.springframework.web.client.HttpClientErrorExcept
我正在尝试通过基本身份验证获取网址。我设置用户/密码如下。同样的凭证也在 postman 中工作。 String RELATIVE_IDENTITY_URL = "http://my_url/api
我正在尝试向身份验证服务器发送帖子消息,并且我正在使用 postforobject。当我运行程序时,我收到“线程“main”中的异常 org.springframework.web.client.Ht
这是关于使用 rest 模板访问 POST api。我收到 org.springframework.web.client.HttpClientErrorException: 400 null。我正在为
当我抛出 HttpClientErrorException 时,根据下面的示例代码,我希望 HTTP 代码为 HTTP 400。相反,我得到一个 HTTP 500 响应代码和消息 400 BAD_RE
我正在从一个模块调用一个服务到另一个模块。但它抛出一个异常: org.springframework.web.client.HttpClientErrorException: 401 null POS
我有网络服务 URL: http://myservice.local/aprovalanduser/?format=json&Name=India 当我使用 resttemplate httpsres
我是 Web 服务的新手。我正在开发一个应用程序,我在客户端使用 AnhularJs1.x 将数据发送到 Spring Rest Controller。 应用程序的架构是基于微服务的。我能够从 ang
我正在与客户端一起设置休息服务器,但几个小时以来我已经遇到了问题。当我调用 getPoints() 方法时,一切正常,但是当我调用 getPoint(Long id)、deletePoint(Long
我使用 spring boot 教程作为基础( https://spring.io/guides/tutorials/spring-boot-oauth2/ ) 测试 Oauth2。 但是,我的身份验
我为 FilterDataController 编写了测试。但是我在执行测试期间出现以下错误。当我手动发送 GET 请求时,我收到了正确的 JSON。 org.springframework.web.
在我的 java 代码中,我尝试使用 RestTemplate 访问微服务,如下所示: headers = new HttpHeaders(); headers.setAccept(A
I am consuming API which has to type of response success response 200 and Bad response 400 both of t
2020年6月12日更新我已经解决了这个问题。问题的原因是我的jetty服务器没有正常启动。我把jdk从14改成了1.7。所有可以吸取的教训是: / 它始终可以接收所有网络请求。 感谢您回答我的问题,
我是一名优秀的程序员,十分优秀!