...
Registers CXFServlet with a "/services/*" URL pattern for serving CXF JAX-WS endpoints.
Please see JAX-WS Spring Boot demo.
Setup
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.7</version> </dependency> |
Examples
Consider the following Configuration instance:
Code Block | ||||
---|---|---|---|---|
| ||||
package sample.ws;
import javax.xml.ws.Endpoint;
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 sample.ws.service.HelloPortImpl;
@Configuration
public class WebServiceConfig {
@Autowired
private Bus bus;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
endpoint.publish("/Hello");
return endpoint;
}
} |
Having a CXF JAX-WS starter alongside this Configuration is sufficient for enabling a CXF JAX-WS endpoint which will respond to SOAP request URI such as
"http://localhost:8080/services/Hello".
Please also see a JAX-WS Spring Boot demo.
Additional Configuration
Use "cxf.path" property to customize a CXFServlet URL pattern.
...
Optionally auto-discovers JAX-RS root resources and providers and creates a JAX-RS endpoint.
Please see JAX-RS Spring Boot and JAX-RS Spring Boot Scan demos.
Setup
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxrs</artifactId> <version>3.1.7</version> </dependency> |
...
Use "cxf.path" property to customize a CXFServlet URL pattern.
JAX-RS root resources and providers annotated with JAX-RS @Path and @Provider and native CXF Providers annotated with CXF @Provider can be auto-discovered.
Use "cxf.jaxrs.component-scan" property to create a JAX-RS endpoint from the auto-discovered JAX-RS root resources and providers which are marked as Spring Components (annotated with Spring @Component or created and returned from @Bean methods).
Use "cxf.jaxrs.classes-scan" property to create a JAX-RS endpoint from the auto-discovered JAX-RS root resources and provider classes. Such classes do not have to be annotated with Spring @Component. This property needs to be accompanied by a "cxf.jaxrs.classes-scan-packages" property which sets a comma-separated list of the packages to scan.
Note that while "cxf.jaxrs.component-scan" and "cxf.jaxrs.classes-scan" are mutually exclusive, "cxf.jaxrs.component-scan" can be used alongside the "cxf.jaxrs.classes-scan-packages" property to enable the auto-discovery of the JAX-RS resources and providers which may or may not be marked as Spring Components.
Examples
Manual Configuration
Consider the following Configuration instance:
Code Block | ||||
---|---|---|---|---|
| ||||
package sample.rs.service;
import java.util.Arrays;
import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import sample.rs.service.hello1.HelloServiceImpl1;
import sample.rs.service.hello2.HelloServiceImpl2;
@SpringBootApplication
public class SampleRestApplication {
@Autowired
private Bus bus;
public static void main(String[] args) {
SpringApplication.run(SampleRestApplication.class, args);
}
@Bean
public Server rsServer() {
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setBus(bus);
endpoint.setServiceBeans(Arrays.<Object>asList(new HelloServiceImpl1(), new HelloServiceImpl2()));
endpoint.setAddress("/");
endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
return endpoint.create();
}
} |
Having a CXF JAX-RS starter alongside this Configuration is sufficient for enabling a CXF JAX-RS endpoint which will respond to HTTP request URI such as
"http://localhost:8080/services/sayHi/ApacheCxfUser". The above code also makes Swagger docs available at "http://localhost:8080/services/swagger.json".
Please also see a JAX-RS Spring Boot demo.
Auto Configuration
Spring Boot Application example shown in the Manual Configuration section can be simplified if the auto-discovery is enabled.
For example, given the following application.properties:
Code Block | ||||
---|---|---|---|---|
| ||||
cxf:
jaxrs:
component-scan: true
classes-scan-packages: org.apache.cxf.jaxrs.swagger |
the application becomes simply:
Code Block | ||||
---|---|---|---|---|
| ||||
package sample.rs.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleScanRestApplication {
public static void main(String[] args) {
SpringApplication.run(SampleScanRestApplication.class, args);
}
} |
This application will enable a CXF JAX-RS endpoint which will respond to HTTP request URI such as
"http://localhost:8080/services/sayHi/ApacheCxfUser". The above code also makes Swagger docs available at "http://localhost:8080/services/swagger.json".
Please also see a JAX-RS Spring Boot Scan demo.