使用maven: Apache CXF – Using CXF with maven
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-jdk14</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
@WebService
public interface HelloService {
String sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
public class CXFDemo {
public static void main(String[] args) {
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
String address = "http://localhost:8000/hello";
jaxWsServerFactoryBean.setAddress(address);
jaxWsServerFactoryBean.setServiceClass(HelloServiceImpl.class);
//添加日志输入输出拦截器,观察soap请求 soap响应内容
jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
jaxWsServerFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
Server server = jaxWsServerFactoryBean.create();
server.start();
}
}
客户端访问:
@Test
public void client() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress(ADDRESS);
jaxWsProxyFactoryBean.setServiceClass(HelloService.class);
HelloService helloService = (HelloService) jaxWsProxyFactoryBean.create();
System.out.println(helloService.sayHello("shippuden"));
}
客户端:hello shippuden
服务端:
----------------------------
ID: 1
Address: /hello
Encoding: UTF-8
Content-Type: text/xml; charset=UTF-8
Headers: {SOAPAction=[""], Accept=[*/*], Cache-Control=[no-cache], User-Agent=[Apache CXF 2.2.3], content-type=[text/xml; charset=UTF-8], Host=[localhost:8000], connection=[keep-alive], Pragma=[no-cache], Content-Length=[203], Content-Type=[text/xml; charset=UTF-8]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://cxf.shippuden.wang.com/"><arg0>shippuden</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
九月 18, 2019 12:29:04 上午 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
信息: Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://cxf.shippuden.wang.com/"><return>hello shippuden</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------