webservice详解(CXF)

闲话少说:

1.web.xml添加配置

 <!--add cxf config-->
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <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> 

2.新建cx-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" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:context="http://www.springframework.org/schema/context"
    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
                        http://cxf.apache.org/jaxrs 
                        http://cxf.apache.org/schemas/jaxrs.xsd
                        
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        ">	
    
   
    <!-- 带有接口的发布 -->
    <!-- 
        id:唯一标识
        address:访问url
        serviceClass:接口类型
     -->
   <jaxws:server id="aop" address="/external"
        serviceClass="*.*.*.*.*.service.*" >
        <jaxws:serviceBean>
<!-- 			提供服务的实现类 -->
            <bean class="*.*.*.*.*.service.impl.*Impl"></bean>
        </jaxws:serviceBean>
<!-- 			加入消息拦截器  -->
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
    </jaxws:server>

    <!-- 配置restful方式的web服务 -->
    <bean id="ps" class="*.*.*.*.*.service.impl.*Impl"></bean>
    <jaxrs:server id="get" address="/getMethod">
        <jaxrs:serviceBeans>
            <ref bean="ps"/>
        </jaxrs:serviceBeans>
        <jaxrs:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxrs:outInterceptors>
    </jaxrs:server> 
    
     <import resource="classpath:META-INF/cxf/cxf.xml" />
     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
</beans>

3.webService的接口与实现类

3.1普通的

@WebService
public interface *Service {

	/**
	 * 
	 * 
	 * @param orderId
	 * @param tmsState
	 */
	@WebMethod(operationName = "orderChange")
	@WebResult(name = "result")
	JSONResponse orderChange(@WebParam(name = "key") String key,
			@WebParam(name = "orderId") String orderId,
			@WebParam(name = "tmsState") String tmsState);

调用测试方式:eclipse有自带的一种测试方式,很简单,就不多说了




3.2  restful形式的,可以直接通过网址调用

@Produces({ MediaType.APPLICATION_JSON })
public interface CarriageChangeService extends Serializable {
/**
 * TMS运单状态变更
 * 
 * @param key
 * @param orderId
 * @param tmsState
 * @throws UnsupportedEncodingException
 */
@GET
@Path("/carriageChange/{key}/{orderId}/{tmsState}")
public String carriageChange(@PathParam("key") String key,
@PathParam("orderId") String orderId,
@PathParam("tmsState") String tmsState)
throws UnsupportedEncodingException;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值