Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
package com.programb.webservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.programb.webservice.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
@WebServiceClient(name = "CommonService", targetNamespace = "http://www.programb.com/", wsdlLocation = "http://localhost:8092/services/CommonService?wsdl")
public class CommonService_Service
extends Service
{
private final static URL COMMONSERVICE_WSDL_LOCATION;
private final static WebServiceException COMMONSERVICE_EXCEPTION;
private final static QName COMMONSERVICE_QNAME = new QName("http://www.programb.com/", "CommonService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:8092/services/CommonService?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
COMMONSERVICE_WSDL_LOCATION = url;
COMMONSERVICE_EXCEPTION = e;
}
public CommonService_Service() {
super(__getWsdlLocation(), COMMONSERVICE_QNAME);
}
public CommonService_Service(WebServiceFeature... features) {
super(__getWsdlLocation(), COMMONSERVICE_QNAME, features);
}
public CommonService_Service(URL wsdlLocation) {
super(wsdlLocation, COMMONSERVICE_QNAME);
}
public CommonService_Service(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, COMMONSERVICE_QNAME, features);
}
public CommonService_Service(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CommonService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
@WebEndpoint(name = "CommonServiceImplPort")
public CommonService getCommonServiceImplPort() {
return super.getPort(new QName("http://www.programb.com/", "CommonServiceImplPort"), CommonService.class);
}
@WebEndpoint(name = "CommonServiceImplPort")
public CommonService getCommonServiceImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://www.programb.com/", "CommonServiceImplPort"), CommonService.class, features);
}
private static URL __getWsdlLocation() {
if (COMMONSERVICE_EXCEPTION!= null) {
throw COMMONSERVICE_EXCEPTION;
}
return COMMONSERVICE_WSDL_LOCATION;
}
}
package com.programb.webservice.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
@WebService(name = "CommonService", targetNamespace = "http://www.programb.com/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface CommonService {
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "getUser", targetNamespace = "http://www.programb.com/", className = "com.programb.webservice.client.GetUser")
@ResponseWrapper(localName = "getUserResponse", targetNamespace = "http://www.programb.com/", className = "com.programb.webservice.client.GetUserResponse")
public User getUser(
@WebParam(name = "userName", targetNamespace = "")
String userName);
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "sayHello", targetNamespace = "http://www.programb.com/", className = "com.programb.webservice.client.SayHello")
@ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://www.programb.com/", className = "com.programb.webservice.client.SayHelloResponse")
public String sayHello(
@WebParam(name = "userName", targetNamespace = "")
String userName);
}
package com.programb.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUser", propOrder = {
"userName"
})
public class GetUser {
protected String userName;
public String getUserName() {
return userName;
}
public void setUserName(String value) {
this.userName = value;
}
}
package com.programb.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "gsetUserResponse", propOrder = {
"_return"
})
public class GetUserResponse {
@XmlElement(name = "return")
protected User _return;
public User getReturn() {
return _return;
}
public void setReturn(User value) {
this._return = value;
}
}
package com.programb.webservice.client;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _GetUser_QNAME = new QName("http://www.programb.com/", "getUser");
private final static QName _GetUserResponse_QNAME = new QName("http://www.programb.com/", "getUserResponse");
private final static QName _SayHello_QNAME = new QName("http://www.programb.com/", "sayHello");
private final static QName _SayHelloResponse_QNAME = new QName("http://www.programb.com/", "sayHelloResponse");
public ObjectFactory() {
}
public GetUserResponse createGetUserResponse() {
return new GetUserResponse();
}
public SayHello createSayHello() {
return new SayHello();
}
public GetUser createGetUser() {
return new GetUser();
}
public SayHelloResponse createSayHelloResponse() {
return new SayHelloResponse();
}
public User createUser() {
return new User();
}
@XmlElementDecl(namespace = "http://www.programb.com/", name = "getUser")
public JAXBElement<GetUser> createGetUser(GetUser value) {
return new JAXBElement<GetUser>(_GetUser_QNAME, GetUser.class, null, value);
}
@XmlElementDecl(namespace = "http://www.programb.com/", name = "getUserResponse")
public JAXBElement<GetUserResponse> createGetUserResponse(GetUserResponse value) {
return new JAXBElement<GetUserResponse>(_GetUserResponse_QNAME, GetUserResponse.class, null, value);
}
@XmlElementDecl(namespace = "http://www.programb.com/", name = "sayHello")
public JAXBElement<SayHello> createSayHello(SayHello value) {
return new JAXBElement<SayHello>(_SayHello_QNAME, SayHello.class, null, value);
}
@XmlElementDecl(namespace = "http://www.programb.com/", name = "sayHelloResponse")
public JAXBElement<SayHelloResponse> createSayHelloResponse(SayHelloResponse value) {
return new JAXBElement<SayHelloResponse>(_SayHelloResponse_QNAME, SayHelloResponse.class, null, value);
}
}
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.programb.com/")
package com.programb.webservice.client;
package com.programb.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "programb", propOrder = {
"userName"
})
public class SayHello {
protected String userName;
public String getUserName() {
return userName;
}
public void setUserName(String value) {
this.userName = value;
}
}
package com.programb.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloResponse", propOrder = {
"_return"
})
public class SayHelloResponse {
@XmlElement(name = "return")
protected String _return;
public String getReturn() {
return _return;
}
public void setReturn(String value) {
this._return = value;
}
}
package com.programb.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
"age",
"id",
"name"
})
public class User {
protected Integer age;
protected Long id;
protected String name;
public Integer getAge() {
return age;
}
public void setAge(Integer value) {
this.age = value;
}
public Long getId() {
return id;
}
public void setId(Long value) {
this.id = value;
}
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
}
package com.programb.webservice.config;
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 com.programb.webservice.service.ICommonService;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
ICommonService commonService;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/CommonService");
return endpoint;
}
}
package com.programb.webservice.model;
public class User {
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.programb.webservice.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.programb.webservice.model.User;
@WebService(name = "CommonService",
targetNamespace = "http://www.programb.com/"
)
public interface ICommonService {
@WebMethod
public String sayHello(@WebParam(name = "userName") String name);
@WebMethod
public User getUser(@WebParam(name = "userName") String name);
}
package com.programb.webservice.service.impl;
import org.springframework.stereotype.Component;
import com.programb.webservice.model.User;
import com.programb.webservice.service.ICommonService;
import javax.jws.WebService;
@WebService(serviceName = "CommonService",
targetNamespace = "http://www.programb.com/", //
endpointInterface = "com.programb.webservice.service.ICommonService"
)
@Component
public class CommonServiceImpl implements ICommonService {
@Override
public String sayHello(String name) {
return "Hello ," + name;
}
@Override
public User getUser(String name) {
return new User(1000L, name, 23);
}
}
package com.programb.webservice.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JacksonUtil {
private static ObjectMapper mapper = new ObjectMapper();
public static String bean2Json(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
public static <T> T json2Bean(String jsonStr, TypeReference<T> typeReference) {
try {
return mapper.readValue(jsonStr, typeReference);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
server.port: 8092
spring:
profiles:
active: dev
cxf:
path: /services
logging:
level:
org.springframework.web.servlet: ERROR
spring:
profiles: dev
logging:
level:
ROOT: INFO
com:
xncoding: DEBUG
file: D:/programb/logs/app.log
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.programb</groupId>
<artifactId>springboot-cxf</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-programb-restful</name>
<description>WebService</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>