resteasy自定义参数解析
来源:互联网 发布:吉他软件电脑版 编辑:程序博客网 时间:2024/05/18 15:26
- InjectorFactory:负责创建注入的工厂,比如ValueInject、MethodInjector
- ValueInjector:处理参数解析
要扩展自定义的参数解析,基本思路是这样,使用特定的InejectorFactory创建自定义的ValueInjector。
resteasy框架允许我们使用自已的InjectorFactory,默认是使用InjectorFactoryImpl。如果我们想改变其默认实现,需要在web.xml中指定具体的实现类,这部分的源码在ConfigurationBootstrap#createDeployment方法中,支持自定义的InjectorFactory
web.xml <context-param> <param-name>resteasy.injector.factory</param-name> <param-value>net.dwade.plugins.resteasy.PaymentInjectorFactory</param-value> </context-param>
编写PaymentInjectorFactory实现类,继承InjectorFactoryImpl,重写createParameterExtractor方法,返回自定义的ValueInjector实现类。
PaymentInjectorFactory.java/*** 扩展resteasy的{@link InjectorFactory},用于实现自定义参数解析、参数验证,* 如果使用{@link SpringResteasyBootstrap}作为ServletContextListener启动,默认会使用该InjectorFactory,* 否则需要在web.xml中指定resteasy.injector.factory参数, eg:* <pre>* <context-param>* <param-name>resteasy.injector.factory</param-name>* <param-value>com.sitech.miso.payment.common.support.rest.PaymentInjectorFactory</param-value>* </context-param>* </pre>* @see SpringResteasyBootstrap* @see ResteasyBootstrap* @see ListenerBootstrap#createDeployment()* @author huangxf* @date 2017年4月21日*/public class PaymentInjectorFactory extends InjectorFactoryImpl { @Override public ValueInjector createParameterExtractor(Parameter parameter, ResteasyProviderFactory providerFactory) { boolean isDataBody = hasAnnotation( parameter, RequestDataBody.class ); if ( isDataBody ) { return new RequestDataInjector( parameter, providerFactory ); } return super.createParameterExtractor(parameter, providerFactory); } /** * 返回{@link PaymentMethodInjector} */ @Override public MethodInjector createMethodInjector(ResourceLocator method, ResteasyProviderFactory factory) { return new PaymentMethodInjector( method, factory ); } @SuppressWarnings("rawtypes") protected boolean hasAnnotation( Parameter parameter, Class annotation ) { Annotation[] annotations = parameter.getAnnotations(); if ( annotations == null ) { return false; } for ( Annotation anno : annotations ) { boolean is = annotation.isInstance( anno ); if ( is ) { return true; } } return false; }}
实现ValueInjector接口,完成参数解析的逻辑,这里以获取json报文中的data节点为例。
RequestDataInjector.java/*** 支持resteasy对{@link RequestDataBody}注解的参数解析* @see RequestDataBody* @author huangxf* @date 2017年4月21日*/public class RequestDataInjector implements ValueInjector { private Parameter paramerter; private ResteasyProviderFactory providerFactory; private Logger logger = LoggerFactory.getLogger( this.getClass() ); /** * 保存请求json数据的key,value为JSONObject */ public static final String JSON_KEY = RequestDataInjector.class.getName(); public RequestDataInjector(Parameter parameter, ResteasyProviderFactory providerFactory) { this.paramerter = parameter; this.providerFactory = providerFactory; } @Override public Object inject( HttpRequest request, HttpResponse response ) { Throwable ex = null; try { checkJsonRequest( request ); JSONObject jsonObject = (JSONObject)request.getAttribute( JSON_KEY ); //说明方法中的参数未被解析,因为可能需要对Sign进行解析 if ( jsonObject == null ) { //获取请求数据中的json String json = JSON.parseObject( request.getInputStream(), StandardCharsets.UTF_8, String.class ); logger.info( "解析Json数据:{}", json ); //转换成json对象 jsonObject = JSONObject.parseObject( json ); //保存至上下文中,便于其他参数的解析 request.setAttribute( JSON_KEY, jsonObject ); } //将data节点下面的数据返回 return jsonObject.getJSONObject( "data" ).toJavaObject( paramerter.getType() ); } catch ( Exception e ) { ex = e; logger.warn( "请求参数有误.", e ); } throw new IllegalArgumentException( "Request param error.", ex ); } /** * 检验是否传递的是json数据 */ protected void checkJsonRequest( HttpRequest request ) { String contentType = request.getHttpHeaders().getHeaderString( "Content-Type" ); //匹配application/json,text/json if ( StringUtils.containsIgnoreCase( contentType, "json" ) ) { return; } throw new IllegalArgumentException( "Not json request." ); } @Override public Object inject() { throw new RuntimeException("Not allowed to inject."); }}
阅读全文
1 0
- resteasy自定义参数解析
- resteasy 参数简单解析
- RESTeasy
- Resteasy
- resteasy
- Spring自定义参数解析器
- 自定义view DrawRect()参数解析
- CustomArgumentResolvers 自定义参数解析器无效
- shell自定义函数及参数调用解析
- spring boot 自定义请求参数解析注解
- 通过__setup宏解析bootloader传递上来的自定义参数
- springmvc属性编辑器和自定义参数解析器
- springmvc源码扩展——自定义参数解析
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- k-近邻学习,朴素贝叶斯,期望最大化,最大熵模型算法介绍
- HIVE 排序总结
- for循环输出漏斗的形状【java】
- 操作系统 磁盘调度综合算法
- 安装nginx-http-concat和nginx平滑升級
- resteasy自定义参数解析
- 点击搜索进入预编译状态,searbar跟随导航消失
- equals 和 ==
- Linux文件系统的实现
- 适配器模式
- C++之数组形参
- Android平台车牌识别移动端的原理与应用领域
- 机器学习实战-利用PCA来简化数据
- 基本Kmeans算法介绍及其实现