LoginSignup
6
3

More than 3 years have passed since last update.

非推奨になったErrorControllerのgetErrorPathの対応

Posted at

はじめに

こちらのissues/19844の要約記事

概要

Spring Boot 2.3.0以降から、
404などのエラーハンドリングで使用するErrorControllerのgetErrorPathが非推奨になった。

ErrorController.java
    /**
     * The return value from this method is not used; the property `server.error.path`
     * must be set to override the default error page path.
     * @return the error path
     * @deprecated since 2.3.0 in favor of setting the property `server.error.path`
     */
    @Deprecated
    String getErrorPath();

問題

コンパイル時に警告が出るくらい。
そもそもJavadocに記載されている通りで、該当メソッドをOverrideしても特に意味はない。
( server.error.pathの設定内容が有効なため )

ErrorControllerの実装クラス
    @Override
    public String getErrorPath() {
        return null;
    }

対応

修正対象

警告が気になる場合は@Deprecatedを付与して警告を消す。

ErrorControllerの実装クラス
    @Deprecated
    @Override
    public String getErrorPath() {
        return null;
    }

補足

ErrorControllerは実装しないとデフォルトのBasicErrorControllerが有効になるので注意。

ErrorMvcAutoConfiguration.java
    @Bean
    @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
    public BasicErrorController basicErrorController(ErrorAttributes errorAttributes,
            ObjectProvider<ErrorViewResolver> errorViewResolvers) {
        return new BasicErrorController(errorAttributes, this.serverProperties.getError(),
                errorViewResolvers.orderedStream().collect(Collectors.toList()));
    }
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3