APIJSON(十:AbstractVerifier源码阅读(1))

APIJSON(十:AbstractVerifier源码阅读(1))

2021SC@SDUSC

类名

首先观察一下类名

public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback

注意到这同样是个抽象类,并且继承了两个接口Verifier和IdCallback。

这里抽象类和接口的相关知识可以移步APIJSON(四:AbstractObjectParser源码阅读)

泛型

同时我们注意到,AbstractVerifier和Verifier都用到了泛型——

在SSM框架下,controller要将从逻辑层接收到的数据封装到一个json对象中并返还给客户端,而返回的响应数据的类型不能确定,可能是String、Map、List 等等。

ResponseResult是自定义的一个bean实体类:

public class ResponseResult<T> {

    public static final int STATE_OK= 1 ;
    public static final int STATE_ERROR= -1 ;

    //状态码
    private int state;
    //状态信息描述
    private String message;
    //响应数据
    private T data;

    public void setData(T data) {
        this.data = data;
    }
    //以下为构造、tostring、get、set、hashcode、equals等方法,此处省略
};

Controller:

@RequestMapping("/GetArea.do") 
@ResponseBody
public ResponseResult<String> GetArea(){
	String str = protectedService.getAllPoint();
	System.out.println("Controller -- GetArea -- str :" + str);
	
	ResponseResult<String> rr = new ResponseResult<String>();
	rr.setState(ResponseResult.STATE_OK);
	rr.setData(str);
	return rr;
}

创建对象时<>中的String就是泛型的类型,可以任意指定。需要注意的是泛型需要在创建对象时进行指定,而在当前对象中,指定泛型类型之后,就固定指代对应的类型;

例如:

ResponseResult<String> rr = new ResponseResult<String>();

带有泛型的方法使用时不需要指定泛型,编译器会自己找出具体的类型。泛型方法除了定义不同,调用就像普通方法一样。

上述代码中,由于在创建对象时,<>中指定了泛型为String:

ResponseResult<String> rr = new ResponseResult<String>();

所以此时 T 已经被指定为了String类型。ResponseResult类对应的实体类 rr 中的data属性和setData()方法已经隐式的发生了变化。

private String data;

public void setData(String data) {
    this.data = data;
}

调用setData()方法时,只能传入String类型的参数,若传入其他参数则编译器会报错。

rr.setData(str);

原文链接——JAVA泛型“<T>”的使用

接口Verifier

Verifier接口主要起着校验器的作用,用于验证权限、请求参数、返回结果等。

public interface Verifier<T> {


   boolean verifyAccess(SQLConfig config) throws Exception;


   void verifyRole(String table, RequestMethod method, String role) throws Exception;


   void verifyLogin() throws Exception;

   void verifyAdmin() throws Exception;



   void verifyRepeat(String table, String key, Object value) throws Exception;
   

   void verifyRepeat(String table, String key, Object value, long exceptId) throws Exception;
   

   JSONObject verifyRequest(RequestMethod method, String name, JSONObject target, JSONObject request,
         int maxUpdateCount, String globleDatabase, String globleSchema, SQLCreator creator) throws Exception;


   JSONObject verifyResponse(RequestMethod method, String name, JSONObject target, JSONObject response,
         String database, String schema, SQLCreator creator, OnParseCallback callback) throws Exception;


   @NotNull
   Parser<T> createParser();

   @NotNull
   Visitor<T> getVisitor();
   Verifier<T> setVisitor(@NotNull Visitor<T> visitor);
   
   String getVisitorIdKey(SQLConfig config);

}

verifyAccess(SQLConfig config)用于验证权限是否通过;

verifyRole(String table, RequestMethod method, String role)是由访问者发过来角色名进行验证

verifyLogin()是用于登陆校验

verifyAdmin()是用于管理员角色校验

verifyRepeat(String table, String key, Object value)和verifyRepeat(String table, String key, Object value, long exceptId)都是用来验证是否重复的

verifyRequest(RequestMethod method, String name, JSONObject target, JSONObject request,int maxUpdateCount, String globleDatabase, String globleSchema, SQLCreator creator)是用于验证请求参数的数据和结构

verifyResponse(RequestMethod method, String name, JSONObject target, JSONObject response, String database, String schema, SQLCreator creator, OnParseCallback callback)是用于验证返回结果的数据和结构

接口IdCallback

public static interface IdCallback {
   /**为 post 请求新建 id, 只能是 Long 或 String
    * @param method
    * @param database
    * @param schema
    * @param table
    * @return
    */
   Object newId(RequestMethod method, String database, String schema, String table);


   /**获取主键名
    * @param database
    * @param schema
    * @param table
    * @return
    */
   String getIdKey(String database, String schema, String datasource, String table);

   /**获取 User 的主键名
    * @param database
    * @param schema
    * @param table
    * @return
    */
   String getUserIdKey(String database, String schema, String datasource, String table);
}

newId(RequestMethod method, String database, String schema, String table)为 post 请求新建 id(只能是 Long 或 String)

getIdKey(String database, String schema, String datasource, String table)获取主键名

getUserIdKey(String database, String schema, String datasource, String table)获取 User 的主键名

getIdKey(String database, String schema, String datasource, String table)获取主键名

getUserIdKey(String database, String schema, String datasource, String table)获取 User 的主键名

猜你喜欢

转载自blog.csdn.net/qq_50861917/article/details/121737775