標籤:工程 pre javaee loader out was play 調用介面 encoding
一、服務端
建javaweb工程,去官網下載所需的cxf介面發布的jar包,匯入到工程。官網地址:http://cxf.apache.org/download.html
1、建立調用介面
package com.ymx.cxf.server;import javax.jws.WebService;@WebServicepublic interface UserService { User getUser(String name);}View Code
2、實現介面
package com.ymx.cxf.server;import javax.jws.WebService;@WebServicepublic class UserServiceImpl implements UserService { @Override public User getUser(String name) { User user = new User(); user.setName(name); user.setAge(22); return user; }}View Code
3、配置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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cxfserver</display-name> <!-- cxf服務啟動servlet --> <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>/cxf/*</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-cxf.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>
View Code
4、配置spring-cxf.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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF設定檔,低版本還需引入其他兩個檔案 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- 配置方式 注意:implementor為介面的具體實作類別 --> <jaxws:endpoint implementor="com.ymx.cxf.server.UserServiceImpl" address="/user" ></jaxws:endpoint> </beans>
View Code
5、使用tomcat發布工程,訪問發布的cxf介面的地址
http://10.50.10.52:8080/cxfserver/cxf/user?wsdl
cxfserver發布的工程名,cxf為配置在web.xml中的url-pattern裡面的值,user為配置在spring-cxf.xml中的address。
二、用戶端
1、進入解壓後cxf檔案下的bin目錄,使用wsdl2java命令產生用戶端代碼。
-p 指定其wsdl的命名空間,也就是要產生代碼的包名; -d 指定要產生代碼所在目錄; -client 產生用戶端測試web service的代碼;2、把產生的用戶端代碼匯入工程內,調用服務端代碼如下:
package com.ymx.cxf.client;import javax.xml.namespace.QName;/** * This class was generated by Apache CXF 2.5.9 * 2017-05-20T10:27:29.544+08:00 * Generated source version: 2.5.9 * */public final class UserService_UserServiceImplPort_Client { private static final QName SERVICE_NAME = new QName("http://server.cxf.ymx.com/", "UserServiceImplService"); public static void main(String args[]) throws Exception { UserServiceImplService ss = new UserServiceImplService(UserServiceImplService.WSDL_LOCATION, SERVICE_NAME); UserService port = ss.getUserServiceImplPort(); String name = "test"; User result = port.getUser(name); System.out.println("getUser.result= " + result.getName() + " : " + result.getAge()); }}View Code
使用cxf做webservice介面調用