cxf 发布 一个简单的 webservice

一个 简单的 cxf 发布webservice的例子 ,希望能对你有所帮助。

1,开发环境   eclipse   jdk 1.7   apache-cxf-3.1.6

2,开发步骤

  1).  导入cxf  jar包  我是全部jar包导入了 ,因为 不确定 要哪几个包

          导入spring 的最小必要包,cxf中 有这些包 ,所以 就不必再添加了 

  2).写一个接口  我写的 是  IUserService 接口 代码如下 

package com.wa.webservice.service;

import java.util.List;

import javax.jws.WebService;

import com.wa.webservice.domian.User;
@WebService
public interface IUserService {
public User getUserById(long id);

public List<User> getAllUser();
}

3). 编写实现类,实现刚才的那个接口,UserService  实现 IUerService 代码如下:

package com.wa.webservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.wa.webservice.domian.User;
import com.wa.webservice.service.IUserService;

@WebService(endpointInterface = "com.wa.webservice.service.IUserService", serviceName = "UserService")
public class UserServiceImpl implements IUserService {
@Override
public User getUserById(long id) {
User u = new User("sa", 1, 12);
if (id == 1) {
return u;
}
return null;
}

@Override
public List<User> getAllUser() {
List<User> list = new ArrayList<User>();
User u = new User("dd", 1, 152);
User u1 = new User("saa", 2, 132);
User u2 = new User("ss", 3, 122);
list.add(u2);
list.add(u1);
list.add(u);
return list;
}

}

4). 在web.xml文件中 增加spring ,cxf 支持,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springcxfserver02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>

5). 编写测试类Test1,代码如下:

package com.wa.junit;

import javax.xml.ws.Endpoint;

import com.wa.webservice.service.impl.UserServiceImpl;

public class Test1 {

public static void main(String[] args) {
String address = "http://localhost:9000/UserService";
UserServiceImpl implementor = new UserServiceImpl();
Endpoint.publish(address, implementor);

}
}

6).运行测试类,在浏览器中输入如下地址:http://localhost:9000/UserService?wsdl 

出现一个如下的xml文件 则发布成功

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.webservice.wa.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.webservice.wa.com/" name="UserService" targetNamespace="http://impl.service.webservice.wa.com/">
<wsdl:import location="http://localhost:9000/UserService?wsdl=IUserService.wsdl" namespace="http://service.webservice.wa.com/">
</wsdl:import>
<wsdl:binding name="UserServiceSoapBinding" type="ns1:IUserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUserById">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUserById">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserByIdResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getAllUser">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getAllUser">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getAllUserResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserService">
<wsdl:port binding="tns:UserServiceSoapBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:9000/UserService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 一个 webservice就这样发布成功了!!!如果您觉得此文章对您有所帮助,就点个赞吧!!!

 

posted @ 2016-04-22 16:59  积累+沉淀  阅读(221)  评论(0编辑  收藏  举报