有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导将JAXWS webservice注册为bean

在基于spring boot ws的应用程序中,我按照契约优先的方法创建了一个jax ws Web服务。Web服务已启动,但我无法在Web服务中自动连接其他bean

我如何将我的webservice在春季定义为bean

以下是我的webservice impl类:

@WebService(endpointInterface = "com.foo.bar.MyServicePortType")
@Service
public class MySoapService implements MyServicePortType {
    @Autowired
    private MyBean obj;

    public Res method(final Req request) {
        System.out.println("\n\n\nCALLING.......\n\n" + obj.toString()); //obj is null here
        return new Res();
    }
}

MyServicePortType由maven从wsdl文件生成

当我(通过SoapUi)调用这个服务时,它会给出NullPointerException,因为MyBean对象不是自动连接的

因为我的应用程序是基于Spring boot构建的,所以没有xml文件。目前,我有sun-jaxws.xml个端点配置文件。如何在spring boot应用程序中执行以下配置

<wss:binding url="/hello">
    <wss:service>
        <ws:service bean="#helloWs"/>
    </wss:service>
</wss:binding>

以下是我的SpringBootServletInitializer课程:

@Configuration
public class WebXml extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(WSApplication.class);
    }

    @Bean
    public ServletRegistrationBean jaxws() {
        final ServletRegistrationBean jaxws = new ServletRegistrationBean(new WSServlet(), "/jaxws");
        return jaxws;
    }

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new WSServletContextListener());
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您不必从SpringBootServletializer扩展配置,也不必重写configure()或onStartup()方法。而且你绝不需要构建实现WebApplicationInitializer的东西。只需执行几个步骤(您也可以在单独的@Configuration类中执行所有步骤,使用@springbootplication的类只需要知道这一步在哪里,例如通过@ComponentScan)

    1. 在ServletRegistrationBean中注册CXFServlet
    2. 实例化SpringBus
    3. 实例化JAX-WS SEI(服务接口)的实现
    4. 用SpringBus和SEI实现bean实例化EndpointImpl
    5. 在该EndpointImpl上调用publish()方法

    完成了

    @SpringBootApplication
    public class SimpleBootCxfApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SimpleBootCxfApplication.class, args);
        }
    
        @Bean
        public ServletRegistrationBean dispatcherServlet() {
            return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
        }
    
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }    
    
        @Bean
        public WeatherService weatherService() {
            return new WeatherServiceEndpoint();
        }
    
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService());
            endpoint.publish("/WeatherSoapService");
            return endpoint;
        }
    }