读书人

WebService配备

发布时间: 2012-09-05 15:19:34 作者: rapoo

WebService配置
一、 准备工作。
1、下载apache-cxf的应用包,地址:http://cxf.apache.org/download.html,我选择的是2.4.1版本。

二、发布webservices
1. 新建web project ,并加入apache-cxf-2.4.1\lib所有包,编写要发布的web service 接口和实现.这一步,与前面一样。

1)创建一个接口类,并加上webservice标记

import javax.jws.WebService;

@WebService

public interface HelloWorld {

public String sayHello(String text);

}

2)创建上面这个接口的实现类
import javax.jws.WebService;

@WebService(endpointInterface="test.HelloWorld")

public class HelloWorldImpl implements HelloWorld {

public String sayHello(String text) {

return "Hello" + text ;

}

}

@WebService 注解表示是要发布的web 服务,endpointInterface的值是该服务类对应的接口。

2. 在spring-cxf.xml配置发布的web service

<?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:jaxws="http://cxf.apache.org/jaxws"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<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" />



<bean id="hello" />

<jaxws:endpoint id="helloWorld" implementor="#hello"

address="/HelloWorld" />

</beans>

注意:<jaxws:endpoint id="helloWorld" implementor="#hello"

address="/HelloWorld" />

id:指在spring配置的bean的ID.

Implementor:指明具体的实现类.

Address:指明这个web service的相对地址,

3. 配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>
<!--spring配置文件放在WEB-INF目录下-->

<param-value>/WEB-INF/spring-cxf.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>
<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>/*</url-pattern>

</servlet-mapping>

</web-app>

4.部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/HelloWorld?wsdl,将显示这个web service的wsdl.

注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name>

<url-pattern>/ws/*</url-pattern>

则访问地址为:http://localhost:8080/<web-app-name>/ws/HelloWorld?wsdl
到此webservices就发布成功了。



三、创建一个客户端来调用webservices
1、同样新建java project ,并加入apache-cxf-2.0.7\lib所有包,添加到Build Path;
2、将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的HelloWorld这个类。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:HelloWorld,特别要注意以下两点:
1)接口前面要添加@Webservice的标记,不然会抛出一个 javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.
2)包路径也要一样,不然会抛出一个ClassCastException: $Proxy29 cannot be cast to...
3、配置spring-client.xml
<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

<!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->

<bean id="client"

factory-bean="clientFactory" factory-method="create"/>



<bean id="clientFactory" value="test.HelloWorld"/>
<!-- 这个address一定要注意,正确的-->


<property name="address" value="http://localhost:8080/<web-app-name>/HelloWorld"/>

</bean>

</beans>

4.测试:

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



import test.HelloWorld;



public class Test {



public static void main(String[] args) {



ApplicationContext ctx = new ClassPathXmlApplicationContext(

"spring-client.xml");

HelloWorld client = (HelloWorld) ctx.getBean("client");

String result = client.sayHello("Glen!");

System.out.println(result);

}

}

结果输出“Hello,Glen!”,测试通过,至此一个webservice的调用也成功了。

读书人网 >Web前端

热点推荐