httpservererrorexception.internalservererror不会从http请求rethtemplate捕获错误500

发布于 2025-02-13 19:40:53 字数 1714 浏览 1 评论 0原文

我目前正在开发一项服务,该服务访问另一个团队的另一个服务,该服务在服务没有数据时给出了响应代码500。首先,我可以使用httpservererrorexception.internalsererrorror来捕获异常并获得响应主体。但是,这并不像以前那样起作用。该错误并未被该异常捕获,当我尝试使用catch(异常E)时,它也没有抓住并让它通过它使服务无法正常运行。.

以下是代码,什么是实际上从下面的代码中错误?我应该如何处理500个响应代码?

        Map resultMap1;
        try {
            resultMap1 = restTemplate.postForEntity(url, request, Map.class);
        } catch (HttpServerErrorException.InternalServerError e) {
            resultMap1 = new HashMap();
            resultMap1.put("errorMessage", e.getMessage());
            return resultMap1;
        } catch (HttpServerErrorException.GatewayTimeout e) {
            throw new AppException(
                    GATEWAY_TIMEOUT_CD,
                    GATEWAY_TIMEOUT_MSG +": "+ url,
                    null
            );
        } catch (HttpServerErrorException.BadGateway e) {
            throw new AppException(
                    BAD_GATEWAY_CD,
                    BAD_GATEWAY_MSG +": "+ url,
                    null
            );
        } catch (HttpServerErrorException.ServiceUnavailable e) {
            throw new AppException(
                    SERVICE_UNAVAILABLE_CD,
                    "The service "+ url +" was unavailable.",
                    null
            );
        }
        Map returnMap = new HashMap(); // this is to be the result from my service
        switch (resultMap.get("content_type")){      // In this one, I get NullPointerException      
            // Here would be selection how the content should be treated, different content have different treatment            
        }

我知道实际上可以通过添加其他情况或其他情况来处理NullPoInterException,但是,我很好奇为什么例外情况不会像预定的那样被抓住。

I am currently working on a service that hit another service from another team, which gives the response code 500 when the service has no data to be sent. At first, I can utilize the HttpServerErrorException.InternalServerError to catch the exception and get the response body. However, this did not work as it used to. That error was not caught by that Exception and when I tried using catch (Exception e), it also caught nothing and let it through that makes the service not running correctly..

Below is the code, what is actually wrong from my code below? How should I handle that 500 response code?

        Map resultMap1;
        try {
            resultMap1 = restTemplate.postForEntity(url, request, Map.class);
        } catch (HttpServerErrorException.InternalServerError e) {
            resultMap1 = new HashMap();
            resultMap1.put("errorMessage", e.getMessage());
            return resultMap1;
        } catch (HttpServerErrorException.GatewayTimeout e) {
            throw new AppException(
                    GATEWAY_TIMEOUT_CD,
                    GATEWAY_TIMEOUT_MSG +": "+ url,
                    null
            );
        } catch (HttpServerErrorException.BadGateway e) {
            throw new AppException(
                    BAD_GATEWAY_CD,
                    BAD_GATEWAY_MSG +": "+ url,
                    null
            );
        } catch (HttpServerErrorException.ServiceUnavailable e) {
            throw new AppException(
                    SERVICE_UNAVAILABLE_CD,
                    "The service "+ url +" was unavailable.",
                    null
            );
        }
        Map returnMap = new HashMap(); // this is to be the result from my service
        switch (resultMap.get("content_type")){      // In this one, I get NullPointerException      
            // Here would be selection how the content should be treated, different content have different treatment            
        }

I know that the NullPointerException can actually be handled by adding ELSE or another case, However, I curious about why the Exception wasn't caught well as it supposed to be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文