Tomcat嵌入式开发 (三) Mapping注册及入参处理


简介

本章实现 @RestController标注Controller、@RequestMapping注册url、@RequestBody解析json请求参数、@RequestParam标注请求入参。


创建Application启动类

首先我们需要一个入口用起来配置以及启动Tomcat。先简单的说一下Tomcat启动流程

  1. 初始化Tomcat(基本参数配置)
  2. 扫描启动类对应包下的所有类
  3. 扫描标注了RestController的类
  4. 注册Servlet(添加Handler到context中)
  5. 使用DispatcherServlet 统一处理所有请求,并转发到对应的Handler中进行处理
  6. 启动Tomcat

TomcatStartApplication

public class TomcatStartApplication {

    private static final Log log = LogFactory.getLog(TomcatStartApplication.class);
    
    public static void run(Class<?> primarySource) {

        log.debug(" StartBoot begin run ... ");
        try {

            TomcatStarter starter = TomcatStarter.init();

            //获取启动类对应包下的所有类
            Set<Class<?>> classes = ClassUtil.getChildClass(primarySource.getPackageName());

            //扫描标注了RestController的类
            Set<Class<?>> servletClassSet = ClassUtil.findContainAnnotation(classes, RestController.class);

            //注册Servlet
            ServletRegister servletRegister = new ServletRegister();
            servletRegister.registerServlet(servletClassSet);

            //添加dispatcherServlet统一控制器
            //Tomcat会将所有请求提交到dispatcherServlet,dispatcherServlet再根据uri调用对应的方法
            TomcatStarter.addServlet("dispatcherServlet", new DispatcherServlet(), "/");

            //启动Tomcat
            starter.startUp();

        } catch (LifecycleException e) {
            e.printStackTrace();
            log.error(" StartBoot run fail  ");
        }
    }
}


创建Tomcat启动类

​ 我们创建一个Tomcat的启动类,用于初始化以及启动Tomcat,并提供添加Servlet到Tomcat中的方法。

​ init()方法中的代码都是上一章所讲述过的,这章不重复讲述,这个类只是对上一章的代码简单封装了一下,忘记的同学可以打开上一章博文回顾一下~

public interface WebApplicationInitializer {
    void startUp() throws LifecycleException;
}


public class TomcatStarter implements WebApplicationInitializer{

    public static Tomcat tomcat;
    public static StandardContext defaultContext;
    //默认端口
    public static final int PORT = 8888;
    //默认项目路径
    public static final String CONTEXT_PATH = "";

    public static TomcatStarter init() {
        TomcatStarter starter = new TomcatStarter();
        tomcat = new Tomcat();

        //创建上下文
        defaultContext= new StandardContext();
        defaultContext.setPath(CONTEXT_PATH);
        defaultContext.addLifecycleListener(new Tomcat.FixContextListener());
        tomcat.getHost().addChild(defaultContext);

        Connector connector = new Connector();
        connector.setPort(PORT);
        tomcat.setConnector(connector);

        return starter;
    }

    @Override
    public void startUp() throws LifecycleException {
        tomcat.start();
    }

    public static void addServlet(String servletName, Servlet servlet,String pattern) {
        tomcat.addServlet(CONTEXT_PATH,servletName , servlet);
        defaultContext.addServletMappingDecoded(pattern,servletName);
    }
}


注册Servlet

注意: 由于我们只注册了一个 "/" 路径到Tomcat中,该路径会将所有请求解析到DispatcherServlet,再由DispatcherServlet进行处理请求,请求规则如下图:


这个时候我们就需要把所有类扫描出来,并找出使用@RestController注解标注的类,并把对应所需要调用的方法注册成Handler。

TomcatStartApplication.run()

//获取启动类对应包下的所有类
Set<Class<?>> classes = ClassUtil.getChildClass(primarySource.getPackageName());

//扫描标注了RestController的类
Set<Class<?>> servletClassSet = ClassUtil.findContainAnnotation(classes, RestController.class);

//注册Servlet
ServletRegister servletRegister = new ServletRegister();
servletRegister.registerServlet(servletClassSet);

ServletRegister.class

​ registerServlet方法会遍历传入的Class<?> 集合,判断该类是否使用@RestController注解标注,如未使用则不会将该类的方法注册成Handler。

​ 最终映射的 url = 类上的@RequestMapping + 方法上的@RequestMapping,类可以不添加@RequestMapping注解。

​ 获取到映射的url和方法之后,调用DefaultServletContext.registerHandler()创建Handler,registerHandler()使用一个Map维护url和Handler的关系(用于DispatchServlet根据url调用对应的Handler)。

注意:使用RestController标注并不会注册Handler,仅代表该类为Controller,用于扫描使用;@RequestMapping才会将方法注册成Handler。

public class ServletRegister {

    private static final Log log = LogFactory.getLog(ServletRegister.class);

    public void registerServlet(Set<Class<?>> classes) {

        Iterator<Class<?>> iterator = classes.iterator();
        //遍历所有类
        while (iterator.hasNext()) {

            List<String> classUrl = new ArrayList<>();

            Class<?> c = iterator.next();
            //判断该类是否用@RestController标注
            RestController restAnn = c.getAnnotation(RestController.class);
            if (restAnn == null) {
                continue;
            }
            //判断注解是否添加了RequestMapping注解
            RequestMapping mappingAnn = c.getAnnotation(RequestMapping.class);
            if (null != mappingAnn) {
                classUrl.addAll(Arrays.asList(mappingAnn.value()));
            } else {
                classUrl.add("");
            }

            Method[] methods = c.getMethods();


            //获取最终的请求url,类+方法
            for (String s : classUrl) {
                if (!s.startsWith("/")) {
                    s = "/" + s;
                }

                for (Method method : methods) {
                    RequestMapping reqMethodAnn = method.getAnnotation(RequestMapping.class);
                    //判断方法是否添加mapping注解
                    if (null == reqMethodAnn) {
                        continue;
                    }
                    
                    for (String s1 : reqMethodAnn.value()) {

                        if (!s1.startsWith("/")) {
                            s1 = "/" + s1;
                        }

                        //最终请求路径
                        String pattern = s + s1;

                        String servletName = c.getSimpleName() + "_" + pattern;

                        DefaultServletContext.registerHandler(pattern, new ServletHandler(pattern, method, c, method.getParameters()));

                        log.info("\33[30;2m" + "Servlet  name:" + servletName + "   Mapped add " + pattern + "\33[0m");
                    }
                    
                }
            }

        }
    }
}

DefaultServletContext.class

该类专门用于维护url-Handler映射容器。

public class DefaultServletContext {

    public static Map<String, ServletHandler> handlerMap = new HashMap<>();


    public static ServletHandler getHandler(String servletUri) {
        return handlerMap.get(servletUri);
    }

    public static void registerHandler(String servletUri, ServletHandler handler) {
        handlerMap.put(servletUri, handler);
    }
}


DispatchServlet统一处理

​ 在上一章中,我们使用如下代码去创建Servlet映射,假设我们有1000个Servlet需要注册到Tomcat中,那会加大Tomcat的内存开销。重点来了,这种创建Servlet映射方式是非常不灵活的,对我们日后做AOP也非常不利。所以我们采用DispatchServlet对所有请求进行处理,将请求转发到到对应的Handler中,由Handler执行被代理的方法,并返回执行后的返回值。

//创建Servlet
tomcat.addServlet(CONTEXT_PATH,SERVLET_NAME,new TestServlet());
//servlet映射
context.addServletMappingDecoded("/index",SERVLET_NAME);


DispatchServlet实现Servlet接口,当请求进来时,获取Uri,调用 DefaultServletContext.getHandler(uri)方法获取对应的Handler,执行Handler.handler(req, resp)方法就能获取对应的返回值

public class DispatcherServlet implements Servlet {

    private static final Log log = LogFactory.getLog(DispatcherServlet.class);

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println(JSONObject.toJSONString(servletConfig));
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        //暂不支持模糊匹配
        String uri = req.getRequestURI();
        String requestMethod = req.getMethod();
        log.info(" request uri:"+uri+"   method:"+requestMethod);

        ServletHandler handler = DefaultServletContext.getHandler(uri);

        if (null == handler) {
            resp.setStatus(404);
            return ;
        }

        //根据不同的请求处理
        if (requestMethod.equals(RequestMethod.GET)) {
            //get请求
        } else if (requestMethod.equals(RequestMethod.POST)) {
            //post请求
        }

        try {
            handler.handler(req, resp);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
    }
}


执行Handler

在DispatchServlet中我们能根据uri获取对应的Handler,现在就来看看Handler的代码。

Handler的执行流程:

  1. 获取request中的ParameterMap和body(请求参数)
  2. 初始化Handler代理的方法参数
  3. 为方法参数赋值
  4. invoke执行被代理的方法
  5. 使用ResponseBuilder将返回结果写入到Response中返回出去

往下一点翻翻~讲解为什么要初始化参数

public class ServletHandler {

    private static final Log log = LogFactory.getLog(ServletHandler.class);

    private String uri;
    private Method method;
    private Class<?> c;
    private Parameter[] parameters;

    public ServletHandler(String uri, Method method, Class<?> c, Parameter[] parameters) {
        this.uri = uri;
        this.method = method;
        this.c = c;
        this.parameters = parameters;
    }


    public boolean handler(ServletRequest request, ServletResponse response) throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        log.info(" request "+uri);

        HttpServletRequest req = (HttpServletRequest) request;

        String body = ServletParamUtils.getBody(req);
        log.info("body :" + body);

        Map<String, String[]> paramMap = req.getParameterMap();
        log.info("parameter :" + JSONObject.toJSONString(paramMap));


        //初始化Servlet方法入参列表
        List<Object> paramArray = ServletParamUtils.initParameters(parameters);
        //将paramMap和body注入到paramArray中
        ServletParamUtils.setParameters(paramArray, parameters, paramMap,body);


        Object obj = c.getDeclaredConstructor().newInstance();

        Object result = method.invoke(obj,paramArray.toArray());

        new ResponseBuilder(response)
                .characterEncoding(CharacterType.UTF_8)
                .contentType(ContentType.APPLICATION_JSON_UT8)
                .print(JSON.toJSONString(result));


        return true;
    }
}


方法入参处理

​ 我们在Mapping 方法中会用到@RequestBody和@RequestParam注解标识参数,其中@RequestParam就有一个required表示其是否是必须的,如果是必须的我们需要实例化这个方法参数。

注意:Mapping方法现在暂时不支持基本类型,会报方法类型错误

public class ServletParamUtils {

    /**
     * 检查参数是否规范
     * 1. 不允许多个@RequestBody 注解
     *
     * @param parameters
     */
    public static void checkParameters(Parameter[] parameters) {

        //不允许多个RequestBody
        boolean hasRequestBody = false;

        for (Parameter parameter : parameters) {

            //如果该参数添加了RequestBody注解着直接跳过
            if (!hasRequestBody && null != parameter.getAnnotation(RequestBody.class)) {
                hasRequestBody = true;
                continue;
            } else if (hasRequestBody && null != parameter.getAnnotation(RequestBody.class)) {
                throw new InitParametersException(" @RequestBody is not unique");
            }
        }
    }

    /**
     * 获取请求中的parameter参数
     *
     * @param parameters
     */
    public static List<Object> initParameters(Parameter[] parameters) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        List<Object> paramArray = new ArrayList<>();

        //初始化参数列表
        for (int i = 0; i < parameters.length; i++) {
            Parameter parameter = parameters[i];
            Class<?> c = parameter.getType();
            Object obj = null;
            boolean required = true;

            //获取RequestParam注解
            RequestParam paramAnno = parameter.getAnnotation(RequestParam.class);
            if (null != paramAnno) {
                required = paramAnno.required();
            }

            //如果参数是是必须的,则实例化该参数
            if (required) {
                if (!ClassUtil.isBaseType(parameter.getType())) {
                    obj = c.getDeclaredConstructor().newInstance();
                }
            }

            paramArray.add(obj);
        }
        return paramArray;
    }


    /**
     * 为Servlet请求方法参数列表赋值
     *
     * @param paramArray Servlet请求方法参数初始化列表(已初始化参数)
     * @param parameters Servlet请求方法参数列表
     * @param paramMap   请求参数
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static void setParameters(List<Object> paramArray, Parameter[] parameters, Map<String, String[]> paramMap,String body) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        if (!body.isBlank()) {
            for (int i = 0; i < parameters.length; i++) {
                Parameter parameter = parameters[i];
                if (null != parameter.getAnnotation(RequestBody.class)) {
                    Object obj = JSONObject.parseObject(body, parameter.getType());
                    paramArray.set(i, obj);
                    break;
                }
            }
        }

        for (String key : paramMap.keySet()) {
            for (int i = 0; i < parameters.length; i++) {
                //获取方法第i个参数
                Parameter parameter = parameters[i];
                Class<?> paramClass = parameter.getType();

                if (null != parameter.getAnnotation(RequestBody.class)) {
                    continue;
                }

                //判断参数是否是基本类型
                if (ClassUtil.isBaseType(parameter.getType())) {
                    if (parameter.getName().equals(key)) {
                        Method m = null;
                        if (paramClass.equals(String.class)) {
                            m=paramClass.getMethod("valueOf", Object.class);
                        } else {
                            m=paramClass.getMethod("valueOf", String.class);
                        }
                        Object obj = m.invoke(paramClass, paramMap.get(key)[0]);
                        paramArray.set(i, obj);
                        break;
                    }
                }

                //判断该参数是否存在此字段
                Field[] fields = parameter.getType().getDeclaredFields();
                Field field = FieldUtil.hasField(fields, key);
                if (null != field) {
                    //获取初始化好的参数(可能为空)
                    Object obj = paramArray.get(i);
                    if (null == obj) {
                        obj = parameter.getType().getDeclaredConstructor().newInstance();
                    }

                    FieldUtil.setValue(obj, key, paramMap.get(key)[0]);
                    paramArray.set(i, obj);
                    break;
                }
            }
        }
    }

    public static void setBodyParameters(List<Object> paramArray,String body) {
        for (Object o : paramArray) {
            RequestBody bodyAnn = o.getClass().getAnnotation(RequestBody.class);
            if (null != bodyAnn) {
                o = JSONObject.parseObject(body, o.getClass());
                break;
            }
        }
    }

    /**
     * 获取请求中的body参数
     */
    public static String getBody(HttpServletRequest request) throws IOException {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } finally {
            if (null != br) {
                br.close();
            }
        }
        return sb.toString();
    }

}


创建测试Controller

@RestController
@RequestMapping("/param")
public class ParamController {


    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody User user,String userName,Integer num) {
        System.out.println("user:"+JSONObject.toJSONString(user));
        System.out.println("username:"+userName);
        System.out.println("num:" + num);
        return "success";
    }

}


启动

public class StartBootApplication {

    public static void main(String[] args) throws Exception {
        TomcatStartApplication.run(StartBootApplication.class);
    }
}

测试

注意:在postman中,我既填写了params参数,也填写了body参数,这个并不冲突


等等等

由于文章篇幅有限,代码并没有贴全,需要的同学可以留下邮箱~


声明??php $this->options->title() ?>|版权所有,违者必究|如未注明,均为原创|本网站采?a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" target="_blank">BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Tomcat嵌入式开发 (三) Mapping注册及入参处理


海苔胖胖是胖还是不胖呢