標籤:webservice
WeB項目結構
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4C/35/wKioL1Q4yvChNvXmAAFWjQKhGO8904.jpg" title="QQ20141011141348.png" alt="wKioL1Q4yvChNvXmAAFWjQKhGO8904.jpg" />
User.java實體類
public class User {private String username;private String description;//...}
HelloWorld.java介面
@WebServicepublic interface HelloWorld {String sayHi(@WebParam(name="text")String text); String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList);}
HelloWorldImpl.java實現HelloWorld介面
@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")public class HelloWorldImpl implements HelloWorld {Map<Integer, User> users = new LinkedHashMap<Integer, User>();@Overridepublic String sayHi(String text) {return "Hello " + text;}@Overridepublic String sayHiToUser(User user) {users.put(users.size() + 1, user);return "Hello " + user.getUsername();}@Overridepublic String[] SayHiToUserList(List<User> userList) {String[] result = new String[userList.size()]; int i=0; for(User u:userList){ result[i] = "Hello " + u.getUsername(); i++; } return result;}}
webServiceApp.java服務端
public class webServiceApp {public static void main(String[] args) { System.out.println("Starting web service... "); HelloWorldImpl implementor= new HelloWorldImpl(); String address="http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("Web service started");}}
HelloWorldClient.java用戶端
public class HelloWorldClient {//需要先啟動com.demo.webServiceApppublic 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.setUsername("Umgsai"); user.setDescription("test"); System.out.println(hw.sayHiToUser(user)); String sayHi = hw.sayHi("test~~~"); System.out.println(sayHi);}}
啟動服務端程式後,即可在用戶端中調用發布的服務。
可以在瀏覽器中通過 http://localhost:8080/helloWorld?wsdl 來查看發布的服務
將服務整合到Spring中
applicationContext.xml
<?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.xsdhttp://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/ws_cxf/webservice/helloWorld" /></bean></beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ws_cxf</display-name> <welcome-file-list> <welcome-file>default.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>
將項目部署在Tomcat上並啟動,可以通過http://192.168.13.232:8080/ws_cxf/webservice/helloWorld?wsdl 來查看
此時用戶端代碼如下
public class NewHelloWorldClient {//需要先將項目運行在Tomcat上public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");HelloWorld client = (HelloWorld) applicationContext.getBean("client");User user1 = new User(); user1.setUsername("Tony"); user1.setDescription("test"); User user2 = new User(); user2.setUsername("freeman"); user2.setDescription("test"); List<User> userList= new ArrayList<User>(); userList.add(user1); userList.add(user2); String[] sayHiToUserList = client.SayHiToUserList(userList); System.out.println(sayHiToUserList[0]);}}
可以使用CXF的wadl2java 工具來根據url產生用戶端代碼
apache-cxf-3.0.1\bin wadl2java 650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />http://192.168.13.232:8080/ws_cxf/webservice/helloWorld?wsdl
參考 http://www.cnblogs.com/frankliiu-java/articles/1641949.html
出現ClassNotFound的異常時可以到 findjar.com 搜尋
源碼 http://yunpan.cn/cgGzyjBdYGJDF (提取碼:51c5)
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1562522
WebService學習筆記-使用CXF發布Webservice