Apache CXF + Spring Java config (no XML)



尝试使用Tomcat 7 Maven插件和CXF部署JAX-WS端点2.7.8。首先,我不想拥有任何XML配置春季或CXF。我看到了几个博客,文章,使用cxf-servlet.xml和cxfservlet,但没有完全使用Java Config。查看CXFServlet源代码,它在密钥'config-location'下寻找cxf-servlet.xml或Servlet上下文中的任何内容。我尝试以编程方式注册端点,而不是在cxf-servlet.xml中进行注册,但它不起作用。访问服务时,我会得到404。有什么想法吗?

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

此处发布的所有内容都不是100%XML配置 - 所有帖子都使用classPath:meta -inf/cxf/cxf/cxf.xml,该帖子也用于网络上的大多数教程中。但是有一个解决方案:定义org.apache.cxf.bus.spring.spring.springbus as @bean and configure name = bus.default_bus_id,从org.apache.cxf.bus.

开始。

如其他答案中所述,必须实例化org.apache.cxf.jaxws.endpointimpl-包括beans springbus和sei -implement类的转发。另外,publish() - 端点方法的方法必须构成构成,包括一个包含URL结束的字符串:

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

如果您想与Springboot一起了解有关Apache CXF的更多信息,我建议您查看此GitHub项目。

所需的只是上面的endpoint.publish()调用。

这个线程肯定会使我走上正确的轨道,以使CXF以纯Spring Java配置运行,但是它没有提供所需的所有内容。

对于我的自我,纯Java配置意味着没有web.xml文件,我认为此答案假定存在。例如,弹簧引导不使用web.xml文件。

因此,在不使用任何XML文件的情况下注册CXF端点,您将需要一个配置文件,该文件还加载CXFServlet

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
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 javax.xml.ws.Endpoint;
@Configuration
@ImportResource({"classpath:META-INF/cxf/cxf.xml"})
public class JavaConfiguration {
    @Autowired
    private Bus bus;
    @Bean
    public Endpoint myServiceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, new MyService());
        endpoint.publish("/myservice");
        return endpoint;
    }
    @Bean
    public ServletRegistrationBean cxfServlet() {
        ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*");
        servlet.setLoadOnStartup(1);
        return servlet;
    }
}

以上是成功加载CXF端点所需的所有配置。

我还创建了一个小型项目,可以证明这一点。

我相信,如果您将豆子传递到工厂内。

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.<Object> asList(editionRest()));
    factory.setProviders(Arrays.<Object> asList(provider));
    return factory.create();
}
@Bean
public JacksonJsonProvider jsonProvider() {
    return new JacksonJsonProvider();
}
}

如果您使用的是Spring Boot,则可以使用:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>${cxf.version}</version> <!-- 3.1.7 or higher -->
</dependency>

添加端点:

import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebServiceConfig {
    @Bean
    public Endpoint endpoint(Bus cxfBus) {
        EndpointImpl endpoint = new EndpointImpl(cxfBus, new Calculator());
        endpoint.publish("/CalculatorService");
        return endpoint;
    }
}

CXF-Boot集成的正式文档。

最新更新