Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 異常解決

Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration required a bean of type 'xxx.DispatcherServletPath' that could not be found異常解決

一. 異常問題

我在利用Web Service進行RPC遠程接口調用的時候,需要配置註冊一個CXFServlet到web容器中,代碼如下:

 
package com.yyg.boot.config;

import com.yyg.boot.service.MyService;
import com.yyg.boot.service.impl.MyServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/5/9
 * @Description Description
 */
@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myService());
        endpoint.publish("/api");
        return endpoint;
    }

}

結果產生了如下異常信息:

"C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" -Dvisualvm.id=10230579134500 "-javaagent:E:\JetBrains\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=51229:E:\JetBrains\IntelliJ IDEA 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program 
......
......

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

......

***************************

APPLICATION FAILED TO START

***************************

Description:

Parameter 0 of method errorPageCustomizer in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.

The following candidates were found but could not be injected:

 - Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.


Process finished with exit code 1

二. 原因分析

我們分析一下原因,其實從log信息中仔細看一下,會發現原因如下:

原因就是在DispatcherServlet Registration的註冊中找不到需要用的dispatcherServlet!

按理說dispatcherServlet在SpringBoot中會默認創建並註冊,這裏怎麼突然找不到了呢?之前都可以的!而這個錯誤是在我創建了這個配置類之後產生的,所以說嘛問題是我上面的代碼造成的。

3. 問題解決

後來我仔細分析一下,原來是我的類中,默認的配置方法名有問題:

解決辦法如下:

很簡單,就是把方法名稱換一下,隨便換成別的名稱即可。

package com.yyg.boot.config;

import com.yyg.boot.service.MyService;
import com.yyg.boot.service.impl.MyServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/5/9
 * @Description Description
 */
@Configuration
public class CxfConfig {

    //注意:該方法的名稱不能使用dispatcherServlet(),否則會導致ErrorMvcAutoConfiguration錯誤.
    //org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
    //required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath'
    // that could not be found.
//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
//    }

    @Bean
    public ServletRegistrationBean createServletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myService());
        endpoint.publish("/api");
        return endpoint;
    }

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章