webservice总结之cxf

java webservice有很多种实现方式,处理方式一般有两种:

  a:基于SOAP的 JAX-WS(Java API for XML-Based Web Services) 比如:RI,Axis,CXF

  b:基于REST的JAX-RS 比如Jersey,RESTEasy,CXF

可以看到CXF 不仅用于开发基于 SOAP 的 WS,也开发于开发基于 REST 的 WS,那就来总结一下CXF的具体用法吧。

SOAP 风格写法

1,定义接口

import javax.jws.WebService;

@WebService
public interface HelloFacade {
    String sayHello(String msg);

}

 

2,服务端实现接口与发布

 a, 实现接口:

技术分享
import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.bresume.core.ws.HelloFacade;

@WebService
@Component
public class HelloFacadeImpl implements HelloFacade{

    @Override
    public String sayHello(String msg) {
        System.out.println("msg="+msg);
        return msg+"_success";
    }

}
HelloFacadeImpl

 b, web.xml配置:

技术分享
    <!-- CXF -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
web.xml

 c,spring配置:

  最好将ws配置文件单独放置,引入

 <import resource="spring-cxf.xml"/>

  建立spring-cxf.xml   jaxws:server 或者 jaxws:endpoint 两种配置方式都可以

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/context 
                         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                         http://cxf.apache.org/jaxws
                           http://cxf.apache.org/schemas/jaxws.xsd">




    <!-- <jaxws:server id="helloFacade" address="/soap/hello">
        <jaxws:serviceBean>
            <ref bean="helloFacadeImpl" />
        </jaxws:serviceBean>
    </jaxws:server>
 -->
 
 <jaxws:endpoint id="helloFacade" implementor="#helloFacadeImpl" address="/soap/hello"/>

</beans>

服务端配置完毕,访问wsdl,可看到如下内容,则表示服务端配置成功

3,客户端访问

技术分享
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.bresume.core.ws.HelloFacade;

public class Test {
    static final String url = "http://localhost:8081/ws/soap/hello";

    public static void main(String[] args) {
        testJaxWsClient();
        //testDynamicClient();
    }

    public static void testJaxWsClient () {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(url);
        factory.setServiceClass(HelloFacade.class);

        HelloFacade hellofacade = factory.create(HelloFacade.class);
        String result = hellofacade.sayHello("word");
        System.out.println("testJaxWsClient"+result);
    }

    public static void testDynamicClient() {
        JaxWsDynamicClientFactory  factory = JaxWsDynamicClientFactory .newInstance();
        Client client = factory.createClient("http://localhost:8081/ws/soap/hello?wsdl");

        try {
            Object[] results = client.invoke("sayHello", "world");
            System.out.println("DynamicClientFactory"+results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Test

 -----------未完------

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。