cxf 相关问题

cxf不能将String的参数序列化的问题解决

问题:

java.lang.NoSuchMethodError: org.codehaus.jettison.AbstractXMLStreamWriter.serializeAsArray(Ljava/lang/String;)V
    at org.apache.cxf.jaxrs.provider.json.utils.JSONUtils.createStreamWriter(JSONUtils.java:90)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.createWriter(JSONProvider.java:507)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.marshal(JSONProvider.java:477)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.marshal(JSONProvider.java:527)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.writeTo(JSONProvider.java:371)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.writeTo(JAXRSOutInterceptor.java:298)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:258)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:146)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:84)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:211)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:154)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:130)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:221)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:197)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
    at java.lang.Thread.run(Thread.java:662)
2013-7-1 9:12:40 org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage
警告: Error serializing the response, please check the server logs, response class : Result.
View Code

解决:删除asm-3.1.jar、(另外删除jettison-1.1.jar、jettison-1.3.2.jar中的1.1)

 

cxf异常处理

 在CXF中默认的异常处理是无法满足开发需求,经常需要把异常信息转换为JSON或者XML格式,返回给客户的,这样就只能自定义异常处理了。

    在对外提供接口的时候,如果出现异常,一般会通过JSON或XML将异常码及异常信息反回给客户,这在CXF中也很容易实现。

 

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

 

 

Java代码  收藏代码
  1. @Provider  
  2. public class InvokeFaultExceptionMapper implements ExceptionMapper {  
  3.   
  4.     public Response toResponse(Throwable ex) {  
  5.          StackTraceElement[] trace = new StackTraceElement[1];  
  6.             trace[0] = ex.getStackTrace()[0];  
  7.             ex.setStackTrace(trace);  
  8.             ResponseBuilder rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR);  
  9.             rb.type("application/json;charset=UTF-8");  
  10.             rb.entity(ex);  
  11.             rb.language(Locale.SIMPLIFIED_CHINESE);  
  12.             Response r = rb.build();  
  13.             return r;  
  14.     }  
  15.   
  16. }  

 

第二部:加载InvokeFaultExceptionMapper

 

Xml代码  收藏代码
  1. <jaxrs:server id="expGateway" address="/expGateway">  
  2.         <jaxrs:inInterceptors>  
  3.            <ref bean="inMessageInterceptor"/>  
  4.         </jaxrs:inInterceptors>  
  5.         <jaxrs:outInterceptors>  
  6.             <ref bean="outMessageInterceptor"/>  
  7.         </jaxrs:outInterceptors>  
  8.         <jaxrs:serviceBeans>  
  9.             <ref bean="expGatewayImpl" />  
  10.         </jaxrs:serviceBeans>  
  11.         <jaxrs:extensionMappings>  
  12.             <entry key="json" value="application/json" />  
  13.             <entry key="xml" value="application/xml" />  
  14.         </jaxrs:extensionMappings>  
  15.         <jaxrs:languageMappings>  
  16.             <entry key="en" value="en-gb" />  
  17.         </jaxrs:languageMappings>  
  18.         <jaxrs:providers>  
  19.             <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>  
  20.             <bean class="com.pml.service.outer.InvokeFaultExceptionMapper"/>  
  21.         </jaxrs:providers>  
  22.     </jaxrs:server>  

 

CXF拦截器用法

http://yufenfei.iteye.com/blog/1688133

 

Apache CXF generates Number instead of String

You can add SimpleConverter to your JSONProvider so that it will not convert strings to numbers. By Default it uses DefaultConverter. But make sure that relevant bean as type string for the field.

1 <bean id="simpleConverter" class="org.codehaus.jettison.mapped.SimpleConverter"/>
2 
3 <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
4     <property name="typeConverter" ref="simpleConverter"/>
5 </bean>
View Code
You can find more information about this defect in Jettison http://jira.codehaus.org/browse/JETTISON-61http://jira.codehaus.org/browse/JETTISON-36

 

 cxf 请求参数为json不带类名头不识别的问题解决

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
     xmlns:jaxrs="http://cxf.apache.org/jaxrs"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/util  
     http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://cxf.apache.org/core
     http://cxf.apache.org/schemas/core.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">
 
     <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
     <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
 
     <util:list id="jsonKeys">
         <value>app</value>
         <value>comment</value>
     </util:list>
 
     <util:list id="jsonTypes">
         <value>application/json</value>
         <value>application/jettison</value>
     </util:list>
 
     <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
         <!--
             <property name="dropRootElement" value="true" /> <property
             name="dropCollectionWrapperElement" value="true" /> <property
             name="supportUnwrapped" value="true" /> <property
             name="serializeAsArray" value="true" /> <property
             name="attributesToElements" value="true"/>
         -->
         <property name="serializeAsArray" value="true" />
         <property name="arrayKeys" ref="jsonKeys" />
         <property name="produceMediaTypes" ref="jsonTypes" />
         <property name="consumeMediaTypes" ref="jsonTypes" />
         <property name="ignoreNamespaces" value="true" />
         <property name="dropRootElement" value="true" />
         <property name="dropCollectionWrapperElement" value="true" />
         <property name="ignoreMixedContent" value="true" />
         <property name="supportUnwrapped" value="true" />
         <property name="attributesToElements" value="true" />
     </bean>
View Code

 

 

REFERENCES:http://hdxiong.iteye.com/blog/1752036

http://stackoverflow.com/questions/10895982/apache-cxf-generates-number-instead-of-string

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

posted @ 2013-06-28 09:54  Atlas's blog  阅读(3043)  评论(0编辑  收藏  举报