resteasy经验谈
Posted 菩提树下的杨过
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了resteasy经验谈相关的知识,希望对你有一定的参考价值。
一、如何用fastjson替换默认的jackson
默认情况下,resteasy使用jackson和jaxb来实现json及xml格式的序列化。应用启动时,会自动扫描带@Provider的包,找到最合适的provider。fastjson也提供了jax-rs的Provider实现,如果希望使用fastjson来替换默认的jackson,可以按如下步骤操作:
1.1、去掉默认的jackson-provider以及jaxb-provider依赖
即:
// compile \'org.jboss.resteasy:resteasy-jackson-provider:3.0.14.Final\'
// compile \'org.jboss.resteasy:resteasy-jaxb-provider:3.0.16.Final\'
把这二个依赖jar包去掉,同时记得添加最新的fastjson依赖(1.2.9+版本)
1.2、修改web.xml
1 <web-app id="WebApp_ID" version="2.4" 2 xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 4 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 <display-name>Restful Web Application</display-name> 6 7 <context-param> 8 <param-name>resteasy.resources</param-name> 9 <param-value>com.cnblogs.yjmyzz.ProductService</param-value> 10 </context-param> 11 12 <context-param> 13 <param-name>resteasy.scan.providers</param-name> 14 <param-value>false</param-value> 15 </context-param> 16 17 <context-param> 18 <param-name>resteasy.providers</param-name> 19 <param-value>com.alibaba.fastjson.support.jaxrs.FastJsonProvider</param-value> 20 </context-param> 21 22 <listener> 23 <listener-class> 24 org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap 25 </listener-class> 26 </listener> 27 28 <servlet> 29 <servlet-name>resteasy-servlet</servlet-name> 30 <servlet-class> 31 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 32 </servlet-class> 33 </servlet> 34 35 <servlet-mapping> 36 <servlet-name>resteasy-servlet</servlet-name> 37 <url-pattern>/*</url-pattern> 38 </servlet-mapping> 39 40 </web-app>
解释一下:
resteasy.scan.providers:false 是为了防止resteasy自动扫描@Provider的类
resteasy.providers:com.alibaba.fastjson.support.jaxrs.FastJsonProvider 这里指定了使用fastjson来解析json.
二、输出非UTF-8编码格式的问题
默认情况下,fastjson是采用UTF-8的,详情见 com.alibaba.fastjson.support.config.FastJsonConfig#FastJsonConfig 源码
1 public FastJsonConfig() { 2 3 this.charset = Charset.forName("UTF-8"); 4 5 this.serializeConfig = SerializeConfig.getGlobalInstance(); 6 this.parserConfig = new ParserConfig(); 7 8 this.serializerFeatures = new SerializerFeature[0]; 9 this.serializeFilters = new SerializeFilter[0]; 10 this.features = new Feature[0]; 11 }
所以,就算在REST服务的Procuces里指定了其它编码也没用
@Path("/product") @Produces({"application/json; charset=GBK"}) public class ProductService { //... }
解决办法:又到了我大OOP出场的时候
package com.cnblogs.yjmyzz; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import java.nio.charset.Charset; /** * Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 2017/4/24. */ public class FastJsonConfigGBK extends FastJsonConfig { public FastJsonConfigGBK() { super(); setCharset(Charset.forName("GBK")); setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); } }
从FastJsonConfig派生出一个默认编码为GBK的子类FastJsonConfigGBK,然后再来一个
package com.cnblogs.yjmyzz; import com.alibaba.fastjson.support.jaxrs.FastJsonProvider; /** * Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 2017/4/24. */ public class FastJsonProviderGBK extends FastJsonProvider { public FastJsonProviderGBK() { super(); setFastJsonConfig(new FastJsonConfigGBK()); } }
最后在web.xml中,参考下面调整:
<context-param> <param-name>resteasy.providers</param-name> <param-value>com.cnblogs.yjmyzz.FastJsonProviderGBK</param-value> </context-param>
大功告成。
三、405 Method Not Allowed无响应信息的处理
resteasy有一套默认的异常处理机制,但默认情况下并未处理405状态的异常,见下表:
Exception | HTTP Code | Description |
---|---|---|
ReaderException | 400 | All exceptions thrown from MessageBodyReaders are wrapped within this exception. If there is no ExceptionMapper for the wrapped exception or if the exception isn\'t a WebApplicationException, then resteasy will return a 400 code by default. |
WriterException | 500 | All exceptions thrown from MessageBodyWriters are wrapped within this exception. If there is no ExceptionMapper for the wrapped exception or if the exception isn\'t a WebApplicationException, then resteasy will return a 400 code by default. |
o.j.r.plugins.providers.jaxb.JAXBUnmarshalException | 400 | The JAXB providers (XML and Jettison) throw this exception on reads. They may be wrapping JAXBExceptions. This class extends ReaderException |
o.j.r.plugins.providers.jaxb.JAXBMarshalException | 500 | The JAXB providers (XML and Jettison) throw this exception on writes. They may be wrapping JAXBExceptions. This class extends WriterException |
ApplicationException | N/A | This exception wraps all exceptions thrown from application code. It functions much in the same way as InvocationTargetException. If there is an ExceptionMapper for wrapped exception, then that is used to handle the request. |
Failure | N/A | Internal Resteasy. Not logged |
LoggableFailure | N/A | Internal Resteasy error. Logged |
DefaultOptionsMethodException | N/A | If the user invokes HTTP OPTIONS and no JAX-RS method for it, Resteasy provides a default behavior by throwing this exception |
解决办法:自己定义异常处理ExceptionHandler,参考以下代码:
1 package com.cnblogs.yjmyzz; 2 3 import javax.ws.rs.NotAllowedException; 4 import javax.ws.rs.core.Response; 5 import javax.ws.rs.ext.ExceptionMapper; 6 import javax.ws.rs.ext.Provider; 7 8 /** 9 * Created by yangjunming on 2017/4/26. 10 */ 11 @Provider 12 public class RestExceptionHandler implements ExceptionMapper<NotAllowedException> { 13 14 @Override 15 public Response toResponse(NotAllowedException e) { 16 Response response = Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(e.getMessage()).build(); 17 response.getHeaders().add("Content-Type", "text/plain"); 18 return response; 19 } 20 }
然后在web.xml把这个加上
1 <context-param> 2 <param-name>resteasy.providers</param-name> 3 <param-value>com.cnblogs.yjmyzz.FastJsonProviderGBK,com.cnblogs.yjmyzz.RestExceptionHandler</param-value> 4 </context-param>
参考文章:
http://docs.jboss.org/resteasy/docs/3.1.2.Final/userguide/html_single/#builtinException
以上是关于resteasy经验谈的主要内容,如果未能解决你的问题,请参考以下文章
带有 RestEasy 的 Spring MVC 的 Junit 测试用例
使用 RESTeasy 和 JBoss 的外部 @EJB 注入返回 null
CORS angular js + restEasy on POST
Keycloak 管理客户端 - javax.ws.rs.ProcessingException: RESTEASY003215