使用cxf+javax開發WebService介面及呼叫
阿新 • • 發佈:2018-11-30
1、jar包:cxf-2.5.3.jar neethi-3.0.2.jar wsdl4j-1.6.2.jar xmlschema-core-2.0.2.jar
2、配置檔案:<---cxf.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-3.1.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-servlet.xml" /> <bean id="getServiceImpl" class="com.netmarch.webservice.getServiceImpl" /> <jaxws:endpoint id="getService" implementor="#getServiceImpl" address="/getService" /> </beans> <---applicationContex.xml---> ...... <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext.xml classpath*:cxf.xml </param-value> </context-param> ...... <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping> ......
<---getService.java--->
package com.netmarch.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(targetNamespace = "http://jdk.study.hermit.org/client") public interface getService { @WebMethod(operationName="setProject") @WebResult(name = "result") public String setProject(@WebParam(name = "json") String json);//專案 @WebMethod(operationName="setZl") @WebResult(name = "result") public String setZl(@WebParam(name = "json") String json);//專利介面 }
介面的實現
package com.netmarch.webservice;
public class getServiceImpl implements getService {
//介面的實現
......
}
<---testWebService.java--->
package com.netmarch.webservice; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class text { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //註冊WebService介面 factory.setServiceClass(getService.class); factory.setAddress("http://localhost:8083/webService/getService"); getService getService = (getService)factory.create(); System.out.println("開始呼叫webservice..."); String json = "{...}"; System.out.println("返回的資訊是:"+getService.setZl(json)); } }
專案啟動後,執行main(),即可呼叫service介面。