CXF实现webservice

<p style="margin: 10px auto; font-size: 13px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(245, 245, 245);">1、服务端</p><p style="margin: 10px auto; font-size: 13px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(245, 245, 245);">1、1  DEMO,用于测试传递对象</p>
package com.xq.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="action")
public class Action {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @Column(name="name",length=20, nullable=false)
    private String name;
    @Column(name="url",length=100, nullable=false)
    private String url;
    
    public Action(){}

    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 String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
<span style="font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: rgb(245, 245, 245);">1、2 发布服务的接口</span>
<span style="font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: rgb(245, 245, 245);"></span><pre name="code" class="java">package com.xq.service;

import java.util.ArrayList;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.xq.model.Action;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloService {
    public String sayHi(String name);
    public String findAction(Action action);
    public ArrayList<Action> createActions(int i);
}
 

1、3 接口实现

package com.xq.service.impl;

import java.util.ArrayList;

import javax.jws.WebService;

import com.xq.model.Action;
import com.xq.service.HelloService;

@WebService(endpointInterface = "com.xq.service.HelloService", serviceName = "helloService", portName = "helloServicePort")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHi(String name) {
        // TODO Auto-generated method stub
        return "Hi! " + name;
    }

    @Override
    public String findAction(Action action) {
        // TODO Auto-generated method stub
        System.out.println("invock getAction...");
        System.out.println(action);
        return action.getName();
        
    }

    @Override
    public ArrayList<Action> createActions(int i) {
        ArrayList<Action> actions = new ArrayList<Action>();
        for (int j = 0; j < i; j++) {
            Action action = new Action();
            action.setId(new Long(j));
            actions.add(action);
        }
        return actions;
    }

}
1、4 配置spring

<import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxws:endpoint id="helloService" address="/helloService"
        implementor="com.xq.service.impl.HelloServiceImpl">
        <!-- 加入消息拦截器 -->
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
    </jaxws:endpoint>
1、5 配置web.xml

<!-- spring start -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- spring end -->

    <!-- cxf start -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
    <!-- cxf end -->
2、客户端

用wsdl2java命令生成客户端 wsdl2java http://localhost:8080/cxftest/ws/helloService?wsdl 文件默认生成到wsdl2java.bat目录,如果自定义目录,可以用wsdl2java -d src -client wsdl文件/wsdl的url,目录如下


2、1 写测试类,直接用main方法

public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(HelloService.class);
        factory.setAddress("http://localhost:8080/cxftest/ws/helloService?wsdl");
        HelloService client = (HelloService) factory.create();
        // 以上语句的功能 可以通过spring来实现
        ActionArray actions = client.createActions(5);
        for (int i = 0; i < actions.getItem().size(); i++) {
            System.out.println(actions.getItem().get(i).getId());
        }
        
    }

查看控制台

十月 27, 2014 11:51:41 上午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.xq.com/}HelloServiceService from class com.xq.service.HelloService
0
1
2
3
4



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值