处理 Spring Boot REST 应用程序的 404 错误时出错

2024-01-18

我尝试了 Spring boot 异常处理。 我创建了一个 REST 应用程序,该应用程序适用于所有有效的 url。 我正在尝试处理无效网址的异常。 但如果我尝试使用无效的 url 访问应用程序,我会收到以下异常:-

13:04:02.940 [http-bio-8081-exec-3] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 44 ms
13:04:03.177 [http-bio-8081-exec-3] ERROR o.s.boot.context.web.ErrorPageFilter - Cannot forward to error page for/sample/processgetMessage5 (response is committed), so this response may have the wrong status code
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348) ~[catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) ~[catalina.jar:7.0.55]
    at org.springframework.boot.context.web.ErrorPageFilter.handleErrorStatus(ErrorPageFilter.java:123) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:104) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:89) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.55]

作为 Spring Boot 的新手,我无法找出原因。任何指示或建议都会有所帮助。

想要尝试该网站中提到的选项在此输入链接描述 http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc,一旦我能够删除异常。

任何关于如何在 spring 4 中使用注释处理 404 的指针都会非常有帮助。

尝试下面的代码:-

@Configuration
@EnableWebMvc
@EnableSwagger
public class WebConfig extends WebMvcConfigurerAdapter {
.......

  @Bean
            public EmbeddedServletContainerCustomizer containerCustomizer() {

                return new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {

                    ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                    ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/401.html");
                    ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/401.html");

                    container.addErrorPages(error401Page, error404Page, error500Page);
                }
            };
        }   

从 pom.xml 添加依赖项

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.1.5.RELEASE</version>            
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.1.5.RELEASE</version>            
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

PFB 更新的堆栈跟踪:-

22:09:03.210 [http-bio-8081-exec-3] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 28 ms
22:09:03.413 [http-bio-8081-exec-3] ERROR o.s.boot.context.web.ErrorPageFilter - Cannot forward to error page for/applicationurl/processMessage11 (response is committed), so this response may have the wrong status code
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348) ~[catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) ~[catalina.jar:7.0.55]
    at org.springframework.boot.context.web.ErrorPageFilter.handleErrorStatus(ErrorPageFilter.java:134) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:111) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:58) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:87) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:100) [spring-boot-1.1.5.RELEASE.jar:1.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.55]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.55]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.55]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.55]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:7.0.55

]

该应用程序可以很好地进行有效的 url 映射。它是使用 Spring Boot 构建的。 PFB 应用程序注释类:-

    @EnableJpaRepositories
    @EnableAutoConfiguration
    public class AppConfig {


        public static void main(String[] args) {
            SpringApplication.run(AppConfig .class, args);
        }



@Configuration
@EnableWebMvc
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        converters.add(mappingJackson2HttpMessageConverter);
        converters.add(new StringHttpMessageConverter());

        super.configureMessageConverters(converters);
    }

public class WebApplicationXML extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }   

}

我添加了映射,以便在发生 404 错误时重定向到自定义 html 页面。 PFB 为此所做的更改:

  • 从中删除了注释@EnableWebMvc WebConfig.java 类。这样做是为了消除错误“响应 已提交”,在尝试任何无效的网址时。
  • 在 WebConfig.java 类和 revenant html 页面中添加以下代码:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
    
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
    
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED,
                        "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
                        "/404.html");
                ErrorPage error500Page = new ErrorPage(
                        HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
                ErrorPage error505Page = new ErrorPage(
                        HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/505.html");
                ErrorPage error506Page = new ErrorPage(
                        HttpStatus.METHOD_NOT_ALLOWED, "/405.html");
                container.addErrorPages(error401Page, error404Page,
                        error500Page, error505Page, error506Page);
            }
        };
    }
    

非常感谢您的建议和帮助。这非常有用。

在经历了最初的异常后,我点击了这个链接

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

处理 Spring Boot REST 应用程序的 404 错误时出错 的相关文章

随机推荐

  • 使用 Angular-DataTables 更新数据时会重置分页

    我们有一个使用 Angular DataTables 的 Web 表单 DataTables 1 10 10 Angular datatables v0 5 3 我们使用来自后端的 JSON 来提供数据 该表配置了分页 并且每 10 秒手动
  • 无法从 Amazon Lambda、相同 VPC 和正确的角色权限访问 RDS

    我无法在生产中从 Amazon Lambda 成功连接到 RDS 对于 Amazon Lambda 我使用无服务器框架 离线执行 sls 可以从本地主机连接 RDS 但在生产中 Amazon Lambda 不能 两者都位于同一 VPC 同一
  • symfony2:如何在 QueryBuilder 中使用 group_concat

    我有一个名为 位置 的嵌套集 使用 Gedmo 树 实体 实体 公寓 具有 location id 以及我需要做什么来映射称为 路径 的标量值以返回所有公寓的查询 在 Doctrine1 中 我有这样的代码 Add path to each
  • 以编程方式调用@Controller

    我正在将使用 Spring MVC 控制器实现的代码转换为使用注释构造型 Controller 一切都很顺利 除了一个问题 给定请求 响应 如何以编程方式处理基于注释的控制器的请求 以前 无论实现如何 我可以调用 controller ha
  • Python向用户输入文件路径添加反斜杠和引号,无法打开

    使用Python 3 9 我想 输入 文件路径 通过输入函数 然后我想打开该文件路径 我受到以下错误的阻碍 OSError Errno 22 Invalid argument C Users Hart Documents File txt
  • 如何创建清单文件以使用管理员权限启动应用程序?

    我想为我的 VB 6 0 程序创建一个清单文件 这样当我启动我的应用程序时 操作系统应该要求用户提供管理员权限 我还想知道如何将其嵌入到应用程序中 您实际上并没有在 VB 中创建清单文件 Windows 应用程序清单是一个标准文本文档 格式
  • 如何调试 JVM 中的挂起线程?

    我正在远程 Ubuntu 服务器上运行一个持久的 Java 程序 我在该服务器上拥有 root 用户权限 一段时间后 某些 CPU 核心的使用率会达到 100 日志没有显示任何可疑之处 应用程序仍然可以运行 但吞吐量有所下降 如何调试 JV
  • 从asp.net core高效发送文件

    我有兴趣将一些代码移植到 ASP NET Core 并想知道从 ASP NET Core Web 服务发送文件 也称为 下载 文件 的最有效方法 在我的旧 ASP NET 代码中 我使用的是 FileStream var content n
  • Chrome 64 更新 - 静音标签不再在

    https jsfiddle net kaldenfi rpmk93wm 3 https jsfiddle net kaldenfi rpmk93wm 3 div section section div
  • 将一个数字分成随机不相等的部分[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 所以我试图制作一个脚本 将值 4 随机拆分为 12 个不同的变量 我想不出一个好方法来正确地做到这一点 我考虑过随机化数字 使它们接近
  • 将整数数组传递给 URI 参数中的 WebAPI 方法?

    我有以下内容 HttpDelete public HttpResponseMessage DeleteFolder int ids 我正在尝试使用这个 DELETE http localhost 24144 api folder 1483
  • 如果不适合,如何将“...阅读更多”锚添加到文本末尾

    我正在寻找一种添加方法 read more 如果多行文本的高度超过 8em 则超链接到多行文本可见部分的末尾 我尝试了下面的代码但是 read more不会出现 并且只有最后一行高度的一半可见 如何让它出现并允许用户点击它 就像是 asd
  • 未定义无参数构造函数对象

    我知道这是一个重复的问题 但我找不到我的错误的答案 我试图显示数据库中保存的房间列表 但出现下一个错误 Server Error in Application No parameterless constructor defined for
  • Node.js http-proxy:错误响应未发送到客户端

    我正在使用 proxy web 转发客户端请求 当目标服务器启动时 我的代码将按预期工作 当目标服务器关闭时 ECONNREFUSED 错误将被捕获并打印到 console log 我想将该错误发送回客户端 并尝试使用此处提供的示例 不幸的
  • 通过 SharePoint 功能部署内容类型时 CPU 使用率较高

    我正在创建一个 SharePoint 功能 该功能将用于将某些内容类型 及其自定义列 字段 部署到新的 SharePoint 网站中 我使用了外部工具来生成内容类型的 CAML Andrew Connell 的自定义 STSADM 命令 h
  • 多个 dex 文件定义 Lcom/google/android/gms/internal/zzau

    我收到错误com android dex DexException Multiple dex files define Lcom google android gms internal zzau 当我运行我的应用程序时 gradle 文件是
  • 我的数组中出现双重结果(mysql_fetch_array)

    好的 我执行这个 table get personel table 1 function get personel table id global connection query SELECT query FROM employees q
  • @class 与 #import

    在 Objective C 中使用 class 或 import 有什么区别 我看过各种教程 只有少数使用 class 而大多数其他教程都使用 import class不导入文件 它只是对编译器说 即使你不知道这个类也存在 如果我使用它 请
  • 如何在单元测试中模拟环境文件导入

    在我们的角度应用程序中 我们使用环境文件来加载一些配置 环境 ts export const environment production false defaultLocale en US 然后我们在我们的一项服务中使用它 import
  • 处理 Spring Boot REST 应用程序的 404 错误时出错

    我尝试了 Spring boot 异常处理 我创建了一个 REST 应用程序 该应用程序适用于所有有效的 url 我正在尝试处理无效网址的异常 但如果我尝试使用无效的 url 访问应用程序 我会收到以下异常 13 04 02 940 htt