BasicRESTCmdlet使用说明

功能介绍

BasicRESTCmdlet主要为了化简REST接口设计实现,使用方式模仿SpringMVC,整合ucore之前AbstractRESTCmdlet的模式使用

针对之前的编程模式,主要增加如下的特点:

  • REST参数通过注解注入实现,可以在注解中配置对应的字段名、数据来源(Header或者RequestParam)、验证参数等。
  • 自动输出参数日志、参数验证错误日志等。
  • 返回值可以用之前的AbstractRESTResult,也可以直接返回一个对象,系统自动封装JSONRESTResult对象。

使用范例

基本使用模式如下:

01/*在这里粘贴JAVA代码,包括缩进空格。*/
02@Controller
03 @RESTAnnotate(URL = "/dev/test", Methods = HttpMethod.GET)
04 public class BasicRestTestHandler extends BasicRESTCmdlet {
05 
06     private static transient Logger logger = LoggerFactory.getLogger(BasicRestTestHandler.class);
07 
08 
09     @RESTAPI
10     public Student doTestRequest(
11             @StringParam(value = "name", required = false, validated = true, maxLength = 2)
12             String name,
13             @IntParam(value = "age", source = RequestSource.RequestParam,required = false, min = 0, max = 25)
14             int age,
15             @TypedParam(value = "student", required = false, type = Student.class)
16             Student student,
17  @DateParam(value = "start")
18             Date startDate
19     )
20     {
21 
22         HashMap myMap = new HashMap();
23         myMap.put("name", name);
24         myMap.put("age", age);
25         if (student == null)
26         {
27             logger.info("----> Student is empty");
28         }
29         else
30         {
31             logger.info("----> Student.name: {}, Student.age: {}", student.getName(),
32                     student.getAge());
33         }
34         return student;
35     }
36}

 

 

使用方法与约定

  1. 将REST实现类继承自BasicRESTCmdlet,增加RESTAnnotate、Controller等基本注解
  2. 任意实现一个方法,增加REST API注解(在一个类中目前只支持一个RESTAPI注解)
  3. 方法的参数中,增加IntParam、StringParam、TypedParam等注解,并配置基本参数

 

注解使用说明

IntParam
作用
绑定Integer型数据。
注解声明
01 @Target({ElementType.PARAMETER})
02@Documented
03 @Retention(RetentionPolicy.RUNTIME)
04 public @interface IntParam {
05     String value();
06     RequestSource source() default RequestSource.HeaderParam;
07     RequestParamType paramType() default RequestParamType.Integer;
08     boolean required() default true;
09     boolean validated() default false;
10     int min() default Integer.MIN_VALUE;
11     int max() default Integer.MAX_VALUE;
12}

 

 
常用配置参数说明

value: 绑定到REST请求中的参数名

ResourceSource:选择参数取值的来源,默认为Header,可以配置成RequestParameter

required:配置是否为必选参数。如果为false,并且该参数没有传值,则默认为0

validated:配置是否验证,必须在required为true才能生效。当前验证规则只有一个:最大值和最小值之间的范围,在min和max中配置。

StringParam
作用
绑定Integer型数据
注解声明
01/*在这里粘贴JAVA代码,包括缩进空格。*/
02 @Target({ElementType.PARAMETER})
03@Documented
04 @Retention(RetentionPolicy.RUNTIME)
05 public @interface StringParam {
06     String value();
07     RequestSource source() default RequestSource.HeaderParam;
08     RequestParamType paramType() default RequestParamType.String;
09     boolean required() default true;
10     boolean validated() default false;
11     int maxLength() default Integer.MAX_VALUE;
12}

 

 
常用配置参数说明

value: 绑定到REST请求中的参数名

ResourceSource:选择参数取值的来源,默认为Header,可以配置成HeaderParam、RequestParam

required:配置是否为必选参数。如果为false,并且该参数没有传值,则默认为空字符串。

validated:配置是否验证,必须在required为true才能生效。当前验证规则只有一个:字符串的最大长度

TypedParam
作用
绑定并转换JSON到对象
注解声明
@Target({ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface TypedParam {
    String value();
    RequestSource source() default RequestSource.RequestParam;
    RequestParamType paramType() default RequestParamType.Object;
    boolean required() default true;
    boolean validated() default false;
    Class type() default Object.class;
}
常用配置参数说明

value: 绑定到REST请求中的参数名

ResourceSource:选择参数取值的来源,默认为RequestParameter,即主要通过Post传输。

required:配置是否为必选参数。如果为false,并且该参数没有传值,则改参数默认为空。

validated:目前暂无效。

type:配置要转换的目标对象的类型。

DateParam
作用

绑定并转换DateTime,来源为Long的数字类型或者日期类型字符串

注解声明
@Target({ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface DateParam {
    String value();
    RequestSource source() default RequestSource.HeaderParam;
    RequestParamType paramType() default RequestParamType.Date;
    boolean required() default true;
    boolean validated() default false;
    RequestDateFormat format() default RequestDateFormat.LongFormat;
 
}
常用配置参数说明

value: 绑定到REST请求中的参数名

ResourceSource:选择参数取值的来源,默认为Header,可以配置成RequestParameter

required:配置是否为必选参数。如果为false,并且该参数没有传值,则改参数默认为空。

validated:目前暂无效。

format: 用于参数转换的带格式化字符串类型,可以配置为LongFormat或者StringFormat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值