cxf客户端捕获服务器端异常

一、服务器端配置 


<bean id ="cxfServerExceptionMapper" class="com.iflashbuy.base.util.CxfServerExceptionMapper"/>


	
    <jaxrs:server id="serviceContainer" address="/">
        <jaxrs:features>
            <cxf:logging />
        </jaxrs:features>
        <jaxrs:serviceBeans>
        	<!-- 商品接口 -->
         	
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="feed" value="application/atom+xml" />
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
            <entry key="html" value="text/html" />
        </jaxrs:extensionMappings>
	<jaxrs:providers>
		<ref bean="jacksonProvider" />
		<ref bean="cxfServerExceptionMapper" />
	</jaxrs:providers>
    </jaxrs:server>


服务器端异常处理类

import java.util.Locale;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.ext.ExceptionMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 服务器端异常处理
 * 
 * @author limanman
 * @date 2015年6月15日
 */
public class CxfServerExceptionMapper implements ExceptionMapper<Throwable> {

	private Logger log = LoggerFactory.getLogger(CxfServerExceptionMapper.class);

	@Override
	public Response toResponse(Throwable e) {
		ResponseBuilder rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
		log.error(e.getMessage(), e);
		rb.entity(e.getMessage());
		rb.type("text/x-json;charset=UTF-8");
		rb.language(Locale.SIMPLIFIED_CHINESE);
		Response r = rb.build();
		return r;
	}

}



二、客户端配置


<bean id="cxfClientExceptionMapper" class="com.iflashbuy.base.interceptor.CxfClientExceptionMapper" />
	<!-- 商品模块 -->
	<jaxrs:client id="productRestService"
		serviceClass="com.fengshu.service.product.api.ProductService"
		address="${service_api}">
		<jaxrs:features>
			<cxf:logging />
		</jaxrs:features>
		<jaxrs:providers>
			<ref bean="jsonProvider" />
			<ref bean="cxfClientExceptionMapper" />
		</jaxrs:providers>
	</jaxrs:client>



客户端异常处理类

/**
 * cxf服务器端异常捕获
 * 
 * @author limanman
 * @date 2015年6月15日
 */
public class CxfClientExceptionMapper implements ResponseExceptionMapper<Throwable> {

	@Override
	public Throwable fromResponse(Response r) {

		InputStream is = (InputStream) r.getEntity();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		StringBuilder message = new StringBuilder();
		String line = null;
		try {
			while ((line = br.readLine()) != null) {
				message.append(line);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
			}
		}
		return new ServiceException(message.toString());
	}
}



你可能感兴趣的:(CXF,异常,自定义异常处理,服务器端异常捕获)