当前位置: 首页 > 工具软件 > RESTEasy > 使用案例 >

easyrest java_resteasy经验谈

詹钊
2023-12-01

resteasy 是java体系中比较成熟的rest框架,也是jax-rs规范的实现之一,dubbox的REST服务框架,就是采用的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

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/j2ee4 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

5 Restful Web Application

6

7

8 resteasy.resources

9 com.cnblogs.yjmyzz.ProductService

10

11

12

13 resteasy.scan.providers

14 false

15

16

17

18 resteasy.providers

19 com.alibaba.fastjson.support.jaxrs.FastJsonProvider

20

21

22

23

24 org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap25

26

27

28

29 resteasy-servlet

30

31 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher32

33

34

35

36 resteasy-servlet

37 /*

38

39

40

解释一下:

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 publicFastJsonConfig() {2

3 this.charset = Charset.forName("UTF-8");4

5 this.serializeConfig =SerializeConfig.getGlobalInstance();6 this.parserConfig = newParserConfig();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 classProductService {//...

}

解决办法:又到了我大OOP出场的时候

packagecom.cnblogs.yjmyzz;importcom.alibaba.fastjson.serializer.SerializerFeature;importcom.alibaba.fastjson.support.config.FastJsonConfig;importjava.nio.charset.Charset;/*** Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 2017/4/24.*/

public class FastJsonConfigGBK extendsFastJsonConfig {publicFastJsonConfigGBK() {super();

setCharset(Charset.forName("GBK"));

setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);

}

}

从FastJsonConfig派生出一个默认编码为GBK的子类FastJsonConfigGBK,然后再来一个

packagecom.cnblogs.yjmyzz;importcom.alibaba.fastjson.support.jaxrs.FastJsonProvider;/*** Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 2017/4/24.*/

public class FastJsonProviderGBK extendsFastJsonProvider {publicFastJsonProviderGBK() {super();

setFastJsonConfig(newFastJsonConfigGBK());

}

}

最后在web.xml中,参考下面调整:

resteasy.providers

com.cnblogs.yjmyzz.FastJsonProviderGBK

大功告成。

三、405 Method Not Allowed无响应信息的处理

resteasy有一套默认的异常处理机制,但默认情况下并未处理405状态的异常,见下表:

ExceptionHTTP CodeDescription

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

所以,如果有一个方法,仅允许POST提交,但是用GET请求来访问时,没有任何响应,包括错误码,大多数情况下这不是问题,但是如何用一些url监管系统来检测url是否可访问时,由于没有任何响应,会认为该url无效。

解决办法:自己定义异常处理ExceptionHandler,参考以下代码:

1 packagecom.cnblogs.yjmyzz;2

3 importjavax.ws.rs.NotAllowedException;4 importjavax.ws.rs.core.Response;5 importjavax.ws.rs.ext.ExceptionMapper;6 importjavax.ws.rs.ext.Provider;7

8 /**

9 * Created by yangjunming on 2017/4/26.10 */

11 @Provider12 public class RestExceptionHandler implements ExceptionMapper{13

14 @Override15 publicResponse 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 returnresponse;19 }20 }

然后在web.xml把这个加上

1

2 resteasy.providers

3 com.cnblogs.yjmyzz.FastJsonProviderGBK,com.cnblogs.yjmyzz.RestExceptionHandler

4

参考文章:

http://docs.jboss.org/resteasy/docs/3.1.2.Final/userguide/html_single/#builtinException

 类似资料: