Thomas
Thomas

Reputation: 247

Apache CXF + Spring Java config + replace beans.xml

i try to replace my beans.xml with JavaConfig (Spring). In the beans.xml i have the following configuration:

<bean id="testWebService" class="at.test.TestWebService" />
<jaxws:endpoint id="Test" address="/TestWebService_V100"
    implementor="#testWebService" />

<jaxrs:server id="TestRestService" address="/rest/test"
    name="TestRestService">
    <jaxrs:serviceBeans>
        <ref bean="testWebService" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <ref bean="jsonProvider" />
        <ref bean="DateHandler" />
    </jaxrs:providers>
</jaxrs:server>

at the moment i have a config.java class which contains all beans from the beans.xml.

config.java:

@Configuration
public class Config {

@Bean
public TestWebService testWebService() {

    return new TestWebService();
}

All beans are configured with @Controller and all resources are marked with @Autowired.

@WebService(endpointInterface = "at.test.interfaces.ITestWebService")
@Transactional
@Controller
public class TestWebService extends AbstractSessionWebservice implements
ITestWebService {

It works fine, no errors and the tomcat starts fine. But how do i configure the jaxws:endpoint and jaxrs:server? i have more than one entries in the beans file. So how do i configure multiple jaxws:endpoint and jaxrs:server entries?

Upvotes: 0

Views: 1550

Answers (1)

Shamim
Shamim

Reputation: 13

Please try this

@Autowired
ApplicationContext ctx;


@Bean
public ServletRegistrationBean dispatcherServlet() {
    CXFServlet cxfServlet = new CXFServlet();
    return new ServletRegistrationBean(cxfServlet, "/rest/*");
}

@Bean(name="cxf")
public SpringBus springBus() {
    return new SpringBus();
}

@Bean
    public Server jaxRsServer() {
        List providers = new LinkedList();
    providers.add(new JsonProvider());
    providers.add(new DateHandler());

    LinkedList<ResourceProvider> resourceProviders = new LinkedList<>();
    for (String beanName : ctx.getBeanDefinitionNames()) {
        if (ctx.findAnnotationOnBean(beanName, Path.class) != null) {
            SpringResourceFactory factory = new SpringResourceFactory(beanName);
            factory.setApplicationContext(ctx);
            resourceProviders.add(factory);
        }
    }


    JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
    factory.setBus(springBus());
    factory.setProviders(providers);
factory.setResourceProviders(resourceProviders);
    return factory.create();

    }


} 

And in your TestRestService endpoint,

@Endpoint
public class TestRestServiceEndpoint implements TestRestService {

    @PayloadRoot(localPart=TestWebService_V100, namespace=NAMESPACE)
    public <ReturnType> <MethodName>(<RequestType> request) {
    // WS BL
        return;     
    }

}

Upvotes: 1

Related Questions