Apache CXF + Spring Java config (no XML)

前端 未结 5 2171
孤城傲影
孤城傲影 2020-12-15 19:05

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 severa

5条回答
  •  春和景丽
    2020-12-15 19:51

    I belive that if you pass your beans inside factory.setServiceBeans it will work

    package br.com.app.spring;
    
    import java.util.Arrays;
    
    import javax.ws.rs.ext.RuntimeDelegate;
    
    import org.apache.cxf.bus.spring.SpringBus;
    import org.apache.cxf.endpoint.Server;
    import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
    import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    import br.com.app.JaxRsApiApplication;
    import br.com.app.services.EditionRest;
    import br.com.app.services.EditionService;
    
    @Configuration
    @ImportResource(
        { 
            "classpath:META-INF/cxf/cxf.xml", 
            "classpath:META-INF/cxf/cxf-extension-xml.xml",
            "classpath:META-INF/cxf/cxf-servlet.xml" 
        })
    public class CXFConfig {
    
    @Bean(destroyMethod = "shutdown")
    public SpringBus cxf() {
        return new SpringBus();
    }
    
    @Bean
    public EditionService editionRest() {
        return new EditionRest();
    }
    
    @Bean
    public JaxRsApiApplication jaxRsApiApplication() {
        return new JaxRsApiApplication();
    }
    
    @Autowired
    @Bean
    public Server jaxRsServer(JacksonJsonProvider provider) {
    
        JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(jaxRsApiApplication(), JAXRSServerFactoryBean.class);
        factory.setServiceBeans(Arrays. asList(editionRest()));
        factory.setProviders(Arrays. asList(provider));
    
        return factory.create();
    }
    
    @Bean
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }
    }
    
        

    提交回复
    热议问题