首页 > 代码库 > webservice总结之cxf
webservice总结之cxf
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"; }}
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>
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(); } }}
-----------未完------
webservice总结之cxf
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。