第一个cxf 程序

使用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>
--------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值