700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > cxf 发布 一个简单的 webservice

cxf 发布 一个简单的 webservice

时间:2022-10-09 14:40:35

相关推荐

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="/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /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="/2001/XMLSchema" xmlns:wsdl="/wsdl/" xmlns:tns="http://impl.service./" xmlns:soap="/wsdl/soap/" xmlns:ns2="/soap/http" xmlns:ns1="http://service./" name="UserService" targetNamespace="http://impl.service./">

<wsdl:import location="http://localhost:9000/UserService?wsdl=IUserService.wsdl" namespace="http://service./">

</wsdl:import>

<wsdl:binding name="UserServiceSoapBinding" type="ns1:IUserService">

<soap:binding style="document" transport="/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就这样发布成功了!!!如果您觉得此文章对您有所帮助,就点个赞吧!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。