Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
程序员文章站
2022-07-14 18:40:10
...
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;
}
}