1. 借助 annotation 创建独立启动的web 服务。
准备: 新建工程 导入需要的jar 包35个,如图:
1、 首先服务点接口。
package com.longlong.twsserver; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import com.longlong.twsserver.domain.User; @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList); }2、 服务点实现。
package com.longlong.twsserver.impl; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import com.longlong.twsserver.HelloWorld; import com.longlong.twsserver.domain.User; @WebService(endpointInterface="com.longlong.twsserver.HelloWorld",serviceName="helloWorld") public class HelloWorldImpl implements HelloWorld{ Map<Integer, User> users = new LinkedHashMap<Integer, User>(); public String sayHi(String text) { return "Hello " + text; } public String sayHiToUser(User user) { users.put(users.size()+1, user); return "Hello "+ user.getName(); } public String[] SayHiToUserList(List<User> userList) { String[] result = new String[userList.size()]; int i=0; for(User u:userList){ result[i] = "Hello " + u.getName(); i++; } return result; } }3、 实体。
package com.longlong.twsserver.domain; public class User { private String id; private String name; private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }4、 编写 WebServiceApp.java类来暴露 web服务。
package com.longlong.twsserver.server; import javax.xml.ws.Endpoint; import com.longlong.twsserver.impl.HelloWorldImpl; public class WebServiceApp { public static void main(String[] args) { System.out.println("web service start"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("web service started"); } }
5、run WebServiceApp.java 类来启动服务。 访问 http://localhost:8080/helloWorld?wsdl 查看是否显示wsdl。
package com.longlong.twsserver.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.longlong.twsserver.HelloWorld; import com.longlong.twsserver.domain.User; public class HelloWorldClient { public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); User user = new User(); user.setName("Tony"); user.setDescription("test"); System.out.println(hw.sayHiToUser(user)); } }
7、测试: run WebServiceApp.java 类来启动服务,然后 run HelloWorldClient.java 来访问服务。
二 集成到spring 中。
1.web.xml中加入如下内容:
<?xml version="1.0" encoding="UTF-8"?> <web-app> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/classes/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXFServlet</display-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>/webservice/*</url-pattern> </servlet-mapping> </web-app>
<?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" /> <jaxws:endpoint id="helloWorld" implementor="com.longlong.twsserver.impl.HelloWorldImpl" address="/helloWorld" /> <bean id="client" class="com.longlong.twsserver.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.longlong.twsserver.HelloWorld" /> <property name="address" value="http://localhost:8080/TWSServer/webservice/helloWorld?wsdl" /> </bean> </beans>
3 修改客户端。
package com.longlong.twsserver.client; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.longlong.twsserver.HelloWorld; import com.longlong.twsserver.domain.User; public class HelloWorldClient { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); HelloWorld client = (HelloWorld) context.getBean("client"); User user1 = new User(); user1.setName("Tony"); user1.setDescription("test"); User user2 = new User(); user2.setName("freeman"); user2.setDescription("test"); List<User> userList = new ArrayList<User>(); userList.add(user1); userList.add(user2); String[] res = client.sayHiToUserList(userList); System.out.println(res[0]); System.out.println(res[1]); } }
4 发布工程 启动web服务器(我用 tomcat 6)。
5 访问 http://localhost:8080/TWSServer/webservice/helloWorld?wsdl 查看是否显示 wsdl 。
6 run run HelloWorldClient.java 来访问服务。