至apache官网下载相关的jar包。
一。 编写提供服务的接口以及相关实现类
package com.demo; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import test.User; @WebService public interface HelloWorld { String sayHi(@WebParam(name = "text")String text); String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList); }
package com.demo; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import test.User; @WebService(endpointInterface = "com.demo.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; } }
1. JAVA模拟服务端提供服务代码:
package test; import javax.xml.ws.Endpoint; import com.demo.HelloWorldImpl; public class WebServiceAppServer { 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"); } }
3. JAVA客户端代码实现调用服务接口:
package test; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.demo.HelloWorld; public class HelloWorldNoSpringClient { public static void main(String[] args) { // TODO Auto-generated method stub 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"); System.out.println(hw.sayHiToUser(user)); } }
1. 在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"> <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.demo.HelloWorldImpl" address="/helloWorld" /> <bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.demo.HelloWorld" /> <property name="address" value="http://localhost:8080/CXF/webservice/helloWorld" /> </bean> </beans>
3. 通过获取applicationContext.xml文件中的配置文件来获取相关BEAN,并调用服务端服务。
package test; import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.demo.HelloWorld; public class HelloWorldWithSpringClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld client = (HelloWorld) context.getBean("client"); User user1 = new User(); user1.setName("Tony"); User user2 = new User(); user2.setName("freeman"); 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]); } }
// TODO Auto-generated method stub 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"); System.out.println(hw.sayHiToUser(user));