CXF随笔(二):CXF开发webservice点点

1jar最小依赖

参考:http://gary0416.iteye.com/blog/1222915

 

部署到tomcat,项目启动后可以看到

经试验,不行,可能由于我用的是cxf3.0版本,还是全部jar吧!

2@XmlRootElement(name = "FlowInfo") 注解效果

使用前:

 

 

 

使用后:

 

 

 

3jsr331-api-1.1.1.jar是必须的,利用CXF发布REST服务得用到它。

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd

       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd

       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 

applicationXML.xml文件中加入如上链接,还是不行,报错:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'jaxrs:extensionMappings'. One of '{"http://cxf.apache.org/jaxws":binding, "http://

 cxf.apache.org/jaxws":dataBinding, "http://cxf.apache.org/jaxws":executor, "http://cxf.apache.org/jaxws":features, "http://cxf.apache.org/jaxws":handlers, "http://

 cxf.apache.org/jaxws":inInterceptors, "http://cxf.apache.org/jaxws":inFaultInterceptors, "http://cxf.apache.org/jaxws":invoker, "http://cxf.apache.org/

 jaxws":outInterceptors, "http://cxf.apache.org/jaxws":outFaultInterceptors, "http://cxf.apache.org/jaxws":properties, "http://cxf.apache.org/jaxws":schemaLocations,

 "http://cxf.apache.org/jaxws":serviceFactory}' is expected.

 

参考:http://blog.csdn.net/czp11210/article/details/8874321

3JAXB

JAXB可以相当灵活以支持其他格式,而不仅限于xml格式。

Jettison就是一个开源的JAXB适配器

参考:http://liugang594.iteye.com/blog/1499813

<FlowInfo settleFlowNo="0001">

<regOrg>zhongdeng</regOrg>

</FlowInfo>

 

4cxf把异常信息转换为JSON或者XML格式

参考:http://yufenfei.iteye.com/blog/1717098

 

第一步:编写自己的异常处理类 InvokeFaultExceptionMapper                     

@Provider

public class InvokeFaultExceptionMapper implements ExceptionMapper { 

 

   public Response toResponse(Throwable ex) { 

        StackTraceElement[] trace = new StackTraceElement[1]; 

           trace[0] = ex.getStackTrace()[0]; 

           ex.setStackTrace(trace); 

           ResponseBuilder rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR); 

           rb.type("application/json;charset=UTF-8"); 

            rb.entity(ex); 

            rb.language(Locale.SIMPLIFIED_CHINESE); 

            Response r = rb.build(); 

            return r; 

    } 

 

}

 

第二部:加载InvokeFaultExceptionMapper

<jaxrs:server id="expGateway" address="/expGateway"> 

    <jaxrs:inInterceptors> 

       <ref bean="inMessageInterceptor"/> 

    </jaxrs:inInterceptors> 

    <jaxrs:outInterceptors> 

        <ref bean="outMessageInterceptor"/> 

    </jaxrs:outInterceptors> 

    <jaxrs:serviceBeans> 

        <ref bean="expGatewayImpl" /> 

    </jaxrs:serviceBeans> 

    <jaxrs:extensionMappings> 

        <entry key="json" value="application/json" /> 

        <entry key="xml" value="application/xml" /> 

    </jaxrs:extensionMappings> 

    <jaxrs:languageMappings> 

        <entry key="en" value="en-gb" /> 

    </jaxrs:languageMappings> 

    <jaxrs:providers> 

        <bean class="com.pml.service.outer.InvokeFaultExceptionMapper"/> 

    </jaxrs:providers> 

</jaxrs:server>

5创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)

参考:http://jyao.iteye.com/blog/1346547

猜你喜欢

转载自seven-q.iteye.com/blog/2183490