springboot中配置webservice的认证配置类,项目启动的时候报错如下?

2023-04-24 15:16:09.507 ERROR 21136 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'cxfConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authInterceptor' defined in file [D:\demo\springbootDemo\target\classes\com\suiwei\webservice\AuthInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2023-04-24 15:16:09.531  INFO 21136 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-04-24 15:16:09.545  WARN 21136 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2023-04-24 15:16:09.556  INFO 21136 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-04-24 15:16:09.717 ERROR 21136 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.suiwei.webservice.AuthInterceptor required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:8568', transport: 'socket'

Process finished with exit code 0

配置类

package com.suiwei.config;

import com.suiwei.webservice.AuthInterceptor;
import com.suiwei.webservice.GreetWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.ServletRegistration;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Resource
    private GreetWebService greetWebService;//注入业务实例

    @Resource
    private Bus bus;//注入bus接口实例
    @Resource
    private AuthInterceptor authInterceptor;//认证拦截器

    /**
     * webservice访问的父路径
     * @return
     */
    @Bean
    public ServletRegistrationBean getRegistrationBean(){
        return new ServletRegistrationBean(new CXFServlet(),"/services/*");
    }

    /**
     * 发布服务
     * 设置当前终端
     * @return
     */
    @Bean
    public Endpoint userServiceEndpoint() {
        System.out.println("服务发布");
        //这里指定的端口不能跟应用的端口冲突, 单独指定
        String path = "http://127.0.0.1:9090/greet";

        EndpointImpl userEndpoint = new EndpointImpl(bus, greetWebService);
        userEndpoint.publish(path);
        userEndpoint.getInInterceptors().add(authInterceptor);//访问的拦截器

        System.out.println("服务成功,path: " + path);
        System.out.println(String.format("在线的wsdl:%s?wsdl", path));
        return userEndpoint;
    }
}

拦截器

package com.suiwei.config;

import com.suiwei.webservice.AuthInterceptor;
import com.suiwei.webservice.GreetWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.ServletRegistration;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Resource
    private GreetWebService greetWebService;//注入业务实例

    @Resource
    private Bus bus;//注入bus接口实例
    @Resource
    private AuthInterceptor authInterceptor;//认证拦截器

    /**
     * webservice访问的父路径
     * @return
     */
    @Bean
    public ServletRegistrationBean getRegistrationBean(){
        return new ServletRegistrationBean(new CXFServlet(),"/services/*");
    }

    /**
     * 发布服务
     * 设置当前终端
     * @return
     */
    @Bean
    public Endpoint userServiceEndpoint() {
        System.out.println("服务发布");
        //这里指定的端口不能跟应用的端口冲突, 单独指定
        String path = "http://127.0.0.1:9090/greet";

        EndpointImpl userEndpoint = new EndpointImpl(bus, greetWebService);
        userEndpoint.publish(path);
        userEndpoint.getInInterceptors().add(authInterceptor);//访问的拦截器

        System.out.println("服务成功,path: " + path);
        System.out.println(String.format("在线的wsdl:%s?wsdl", path));
        return userEndpoint;
    }
}
阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题