標籤:
java webservice有很多種實現方式,處理方式一般有兩種:
a:基於SOAP的 JAX-WS(Java API for XML-Based Web Services) 比如:RI,Axis,CXF
b:基於REST的JAX-RS 比如Jersey,RESTEasy,CXF
可以看到CXF 不僅用於開發基於 SOAP 的 WS,也開發於開發基於 REST 的 WS,那就來總結一下CXF的具體用法吧。
SOAP 風格寫法
1,定義介面
import javax.jws.WebService;@WebServicepublic interface HelloFacade { String sayHello(String msg);}
2,服務端實現介面與發布
a, 實現介面:
import javax.jws.WebService;import org.springframework.stereotype.Component;import com.bresume.core.ws.HelloFacade;@WebService@Componentpublic class HelloFacadeImpl implements HelloFacade{ @Override public String sayHello(String msg) { System.out.println("msg="+msg); return msg+"_success"; }}HelloFacadeImpl
b, web.xml配置:
<!-- CXF --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
web.xml
c,spring配置:
最好將ws設定檔單獨放置,引入
<import resource="spring-cxf.xml"/>
建立spring-cxf.xml jaxws:server 或者 jaxws:endpoint 兩種配置方式都可以
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- <jaxws:server id="helloFacade" address="/soap/hello"> <jaxws:serviceBean> <ref bean="helloFacadeImpl" /> </jaxws:serviceBean> </jaxws:server> --> <jaxws:endpoint id="helloFacade" implementor="#helloFacadeImpl" address="/soap/hello"/></beans>
服務端配置完畢,訪問wsdl,可看到如下內容,則表示服務端配置成功
3,用戶端訪問
import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.bresume.core.ws.HelloFacade;public class Test { static final String url = "http://localhost:8081/ws/soap/hello"; public static void main(String[] args) { testJaxWsClient(); //testDynamicClient(); } public static void testJaxWsClient () { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress(url); factory.setServiceClass(HelloFacade.class); HelloFacade hellofacade = factory.create(HelloFacade.class); String result = hellofacade.sayHello("word"); System.out.println("testJaxWsClient"+result); } public static void testDynamicClient() { JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory .newInstance(); Client client = factory.createClient("http://localhost:8081/ws/soap/hello?wsdl"); try { Object[] results = client.invoke("sayHello", "world"); System.out.println("DynamicClientFactory"+results[0]); } catch (Exception e) { e.printStackTrace(); } }}Test
-----------未完------
webservice總結之cxf