码迷,mamicode.com
首页 > Web开发 > 详细

webservice总结之cxf

时间:2015-01-15 21:56:52      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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

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

webservice总结之cxf

标签:

原文地址:http://www.cnblogs.com/whatadiors/p/4226768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!