微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐