加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice之CXF注解实现(一)

发布时间:2020-12-17 00:02:52 所属栏目:安全 来源:网络整理
导读:1.关于CXF(摘自百度百科) Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Trans

1.关于CXF(摘自百度百科)

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

2.下载

http://cxf.apache.org/download.html

3.集成到eclipse

笔者下载的版本是2.7.7,进入apache-cxf-2.7.7/lib目录,将所需jar包复制到工程中,需要的jar包较多,如图:



4.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>PortalService</display-name>
 
  <!-- cfx webSerivice -->
    <servlet>  
    	<servlet-name>cxf</servlet-name>  
    	<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>  
    	<load-on-startup>1</load-on-startup>  
    </servlet>  
    
    <servlet-mapping>  
      <servlet-name>cxf</servlet-name>
      <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>  
    <session-config>  
      <session-timeout>60</session-timeout>  
    </session-config>
</web-app>

5.cxf-servlet.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"
       xmlns:soap="http://cxf.apache.org/bindings/soap"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">


<!-- ;服务接口 -->
   <jaxws:server id="jaxwsService" serviceClass="com.look.service.IService" address="/test">
 <!-- address为服务发布二级地址 完整地址为 /项目发布名称/cfx拦截地址/address   (cfx拦截地址在web.xml中url-pattern标签中配置)  -->
       <jaxws:serviceBean>
         <!--服务实现类 -->
                <bean class="com.look.service.IServiceImpl" />
       </jaxws:serviceBean>
   </jaxws:server>
</beans>


6.IService.java

package com.look.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.protal.model.RequestCondition;

@WebService
public interface IService
{
    @WebMethod
    String test(@WebParam  RequestCondition requestCondition);
 }
网上的例子多是String类型,这里使用了自定义的Java类型,RequestCondition类的代码这里不贴出了,读者可自行建立该bean,添加属性age并添加相应的set、get方法


7.IServiceImpl.java

package com.look.service;

import com.protal.model.RequestCondition;

public class IServiceImpl implements IService
{
    @Override
    public String test(RequestCondition requestCondition)
    {
       return “年龄是:”+requestCondition.getAge();
    }
}


8.部署并测试

将项目部署到web服务器,如tomcat

编写测试类

package com.look.utest;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.look.service.IService;
import com.protal.model.RequestCondition;

public class Test {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
	    factory.getInInterceptors().add(new LoggingInInterceptor());  
	    factory.getOutInterceptors().add(new LoggingOutInterceptor());  
	    factory.setServiceClass(IService.class);  
	    factory.setAddress("http://localhost:8080/PortalService/services/test");  
	    IService client = (IService) factory.create();
	    RequestCondition requestCondition = new RequestCondition();
	    requestCondition.setAge("18");
	    String msg =  client.test(requestCondition);
	    System.out.println(msg);
	}
}
--------------------------测试结果----------------------------
十月 24,2013 4:16:10 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.look.com/}IServiceService from class com.look.service.IService
十月 24,2013 4:16:10 下午 org.apache.cxf.services.IServiceService.IServicePort.IService
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/PortalService/services/test
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*],SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:test xmlns:ns2="http://service.look.com/"><arg0><age>18</age></arg0></ns2:test></soap:Body></soap:Envelope>
--------------------------------------
十月 24,2013 4:16:10 下午 org.apache.cxf.services.IServiceService.IServicePort.IService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[214],content-type=[text/xml;charset=UTF-8],Date=[Thu,24 Oct 2013 08:16:10 GMT],Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:testResponse xmlns:ns2="http://service.look.com/"><return>年龄是:18</return></ns2:testResponse></soap:Body></soap:Envelope>
--------------------------------------
年龄是:18

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读