Apache CXF是一个开源的WebService框架,CXF可以用来构建和开发WebService,些服务可以支持多种协议,比如:SOAP、POST/HTTP、HTTP,这篇文章讲的是SOAP这个协议使用CXF框架整合进入SpringBoot
SpringBoot整合CFX框架
CXF框架
Apache CXF是一个开源的WebService框架,CXF可以用来构建和开发WebService,些服务可以支持多种协议,比如:SOAP、POST/HTTP、HTTP,这篇文章讲的是SOAP这个协议使用CXF框架整合进入SpringBoot
Maven引入
1 2 3 4 5
| <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.7</version> </dependency>
|
config类配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import com.hospital.test.cxf.ws.TestServices; 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.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration public class CxfConfig { @Autowired private TestServices testServices;
@SuppressWarnings("all") @Bean public ServletRegistrationBean wsServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/soap/*"); return bean; }
@Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); }
@Bean public Endpoint testServices() { EndpointImpl endpoint = new EndpointImpl(springBus(), testServices); endpoint.publish("/testServices"); return endpoint; } }
|
实现
TestServices:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService;
@WebService(name = "TestServices", targetNamespace = "http://ws.cxf.test.baby7blog.com/") public interface TestServices {
@WebMethod(operationName="exec") @WebResult(name = "ret", targetNamespace = "http://ws.cxf.test.baby7blog.com/") String exec(@WebParam(name = "in") String in); }
|
TestServicesImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import com.hospital.budget.business.handler.BusinessHandler; import com.hospital.budget.cxf.ws.BudgetServices; import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.soap.SOAPBinding; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
@WebService(serviceName = "BudgetServices", portName = "BudgetServices", targetNamespace = "http://ws.cxf.test.baby7blog.com/", endpointInterface = "com.hospital.budget.cxf.ws.BudgetServices") @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) @Component @Log4j2 public class BudgetServicesImpl implements BudgetServices {
@Autowired BusinessHandler handler;
@Override public String exec(String in) { String ret; try { ret = handler.exec(in); } catch (Exception e) { log.error(e.getMessage()); ret = "<Response><code>-1</code><msg>to be illegal to do sth;</msg></Response>"; } return ret; } }
|
此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/86.html