Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

tomcat - Apache CXF + Spring Java config (no XML)

Trying to deploy a JAX-WS endpoint using Tomcat 7 Maven plugin and CXF 2.7.8. As a matter of preference, I don't want to have any XML config for Spring or CXF. I see several blogs, articles, posts using cxf-servlet.xml and CXFServlet but none whatsoever completely using Java config. Looking into the CXFServlet source code, it looks for the cxf-servlet.xml or anything in the servlet context under the key 'config-location'. I tried programmatically registering the endpoint instead of in cxf-servlet.xml, but it doesn't work; I get a 404 when accessing the service. Any ideas?

@Configuration
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" })
public class CXFConfig {
    @Autowired
    Bus cxfBus;

    // More code

    @Bean
    public Endpoint calculator() {
        EndpointImpl endpoint = new EndpointImpl(cxfBus, new Calculator());
        endpoint.setAddress("/CalculatorService");
        return endpoint;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Everything posted here is not 100% XML configuration free - all posts are using the classpath:META-INF/cxf/cxf.xml, which is also used in most tutorials on the web. But there′s a solution for that: Define a org.apache.cxf.bus.spring.SpringBus as @Bean and configure name = Bus.DEFAULT_BUS_ID, comming from org.apache.cxf.Bus.

As described in the other answers, the org.apache.cxf.jaxws.EndpointImpl has to be instantiated - including forwarding of the Beans SpringBus and the SEI-implementing Class. Also, the publish()-Method of EndpointImpl has to becalled, including a String containing an URL ending:

package de.jonashackt.tutorial.configuration;

import javax.xml.ws.Endpoint;

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.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.codecentric.namespace.weatherservice.WeatherService;
import de.jonashackt.tutorial.endpoint.WeatherServiceEndpoint;

@Configuration
public class WebServiceConfiguration {

    @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;
    }
}

If you want to learn more about Apache CXF together with SpringBoot, I recommend a look on this github project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...