
de.codecentric.cxf.configuration.CxfAutoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-spring-boot-starter Show documentation
Show all versions of cxf-spring-boot-starter Show documentation
Boot starter for SOAP-Webservices with Apache CXF using JAX-WS & JAXB with Annotations only
The newest version!
package de.codecentric.cxf.configuration;
import de.codecentric.cxf.autodetection.WebServiceAutoDetector;
import de.codecentric.cxf.autodetection.WebServiceScanner;
import de.codecentric.cxf.common.BootStarterCxfException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
import java.util.Map;
/**
* While booting up the CXF-Framework and Servlets, we don´t override the Bean "dispatcherServlet" here - because,
* if you want to use a second Servlet (e.g. because you need some REST-Endpoint via the @RestController Annotation),
* you just could use it. Otherwise, those Servlets would override themselfs.
*
* @author jonashecht
*
*/
@Configuration
@ConditionalOnClass(CXFServlet.class)
@PropertySource("classpath:spring-boot-starter-cxf.properties")
@Import({
XmlValidationConfiguration.class,
SoapMessageLoggerConfiguration.class,
TimeLoggingConfiguration.class
})
public class CxfAutoConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(CxfAutoConfiguration.class);
@Value("${soap.service.base.url:/soap-api}")
private String baseUrl;
@Value("${soap.service.publishedEndpointUrl:NOT_SET}")
private String publishedEndpointUrl;
@Value("${cxf.servicelist.title:CXF SpringBoot Starter - service list}")
private String serviceListTitle;
private String serviceUrlEnding = null;
private Object seiImplementation;
@Bean
public WebServiceAutoDetector webServiceAutoDetector(ApplicationContext applicationContext) throws BootStarterCxfException {
return new WebServiceAutoDetector(new WebServiceScanner(), applicationContext);
}
/**
* We mostly want to autoinitialize the Endpoint and the CXFServlet.
* But when in client mode, this isn´t always wanted (e.g. when you are in Client
* only mode and just want to test or call some SOAP services, but not provide
* services on your own.
*
* Because there is (& sadly will be) no @ConditionalOnMissingProperty in Spring Boot
* (https://github.com/spring-projects/spring-boot/issues/4938), we need to use a workaround:
*
* If endpoint.autoinit is NOT set, Endpoint autoinitialization will run.
* If endpoint.autoinit is set to some other value than false, autoinitialization will also run.
*
* Only if endpoint.autoinit = false, the autoinitialization isn´t running.
*/
@Bean
@ConditionalOnProperty(name = "endpoint.autoinit", matchIfMissing = true)
public ServletRegistrationBean cxfDispatcherServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), baseUrl + "/*");
// Add custom Title to CXF´s ServiceList
Map initParameters = servletRegistrationBean.getInitParameters();
initParameters.put("service-list-title", serviceListTitle);
return servletRegistrationBean;
}
// If you don´t want to import the cxf.xml-Springbean-Config you have to setUp this Bus for yourself
//
@Bean(name = Bus.DEFAULT_BUS_ID)
@ConditionalOnProperty(name = "endpoint.autoinit", matchIfMissing = true)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
@ConditionalOnProperty(name = "endpoint.autoinit", matchIfMissing = true)
public Object seiImplementation() throws BootStarterCxfException {
if(seiImplementation == null) {
seiImplementation = webServiceAutoDetector(null).searchAndInstantiateSeiImplementation();
}
return seiImplementation;
}
@Bean
@ConditionalOnProperty(name = "endpoint.autoinit", matchIfMissing = true)
public Endpoint endpoint() throws BootStarterCxfException {
LOG.info("Autodetection successful. Initializing javax.xml.ws.Endpoint based on " + seiImplementation().getClass().getName());
EndpointImpl endpoint = new EndpointImpl(springBus(), seiImplementation());
// CXF JAX-WS implementation relies on the correct ServiceName as QName-Object with
// the name-Attribute´s text and the targetNamespace
// "http://www.codecentric.de/namespace/weatherservice/"
// Also the WSDLLocation must be set
endpoint.setServiceName(webServiceClient().getServiceName());
endpoint.setWsdlLocation(webServiceClient().getWSDLDocumentLocation().toString());
if (publishedEndpointUrl.equals("NOT_SET")) {
endpoint.setPublishedEndpointUrl(webServiceClient().getServiceName().getLocalPart());
} else {
endpoint.setPublishedEndpointUrl(publishedEndpointUrl);
}
// publish the Service under it´s name mentioned in the WSDL inside name attribute (example: )
endpoint.publish(serviceUrlEnding());
return endpoint;
}
@Bean
public Service webServiceClient() throws BootStarterCxfException {
// Needed for correct ServiceName & WSDLLocation to publish contract first incl. original WSDL
return webServiceAutoDetector(null).searchAndInstantiateWebServiceClient();
}
/**
* @return the base-URL, where the WebServices are configured (eihter via property or default-value)
*/
public String baseUrl() {
return baseUrl;
}
/**
* @return the concrete Service URL-ending, where the WebService is configured according to your WSDL´s Service Name
* (e.g. "/Weather" when there is this inside your WSDL: <wsdl:service name="Weather">)
*/
public String serviceUrlEnding() throws BootStarterCxfException {
if (serviceUrlEnding == null) {
serviceUrlEnding = "/" + webServiceClient().getServiceName().getLocalPart();
}
return serviceUrlEnding;
}
/**
* @return the base-URL, where the WebServices are configured (eihter via property or default-value) and appended
* the concrete Service URL-ending, where the WebService is configured according to your WSDL´s Service Name
* (e.g. "/Weather" when there is this inside your WSDL: <wsdl:service name="Weather">)
*/
public String baseAndServiceEndingUrl() throws BootStarterCxfException {
return baseUrl() + serviceUrlEnding();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy