最近又给自己定了一个计划,将现有的测试工具、测试插件逐步转化为一套公共接口,以服务形式供。

哪些优点

1.服务就在那里,能随时调用获取所需的服务或数据;
2.不需要在项目中依赖相关插件了,瘦身了也解耦了;
3.便于平台之间的对接。

实现步骤

1.选择合适的 RESTful 框架;
2.功能封装成接口服务;
3.再次包装或使用服务;

注册案例

以下基于jersey框架
1.封装成接口服务

@POST
    @Path("iRegister")
    @Produces(MediaType.APPLICATION_JSON)
    public Message iRegister(@FormParam("userName") String userName, @FormParam("password") String password, @FormParam("alias") String alias) {
        String code = null;
        String desc = null;
        User user;
        UserDao userDao = new UserDao();
        List<Object> userList;
        Message message = new Message();
        try {
            userList = userDao.selectByField(userName);
            if (userList.size() >= 1) {
                code = ResultCode.FAILURE;
                desc = ResultDesc.USER_EXISTS;
            } else {
                user = new User();
                user.setUserName(userName);
                user.setAlias(alias);
                user.setPassword(password);
                user.setCreateDate(new Timestamp(System.currentTimeMillis()));
                user.setStatus(UserStatus.VALID);
                userDao.addEntity(user);
                code = ResultCode.SUCCESS;
                desc = ResultDesc.REGISTER_SUCCESS;
            }
        } catch (Exception e) {
            code = ResultCode.FAILURE;
            desc = e.getMessage();
        } finally {
            message.setCode(code);
            message.setDesc(desc);
            return message;
        }
    }

2.调用接口服务,可以直接在前端调用,也可以在后端,看具体需求

@POST
    @Path("register")
    public void register(@FormParam("userName") String userName, @FormParam("password") String password, @FormParam("alias") String alias, @Context HttpServletRequest request, @Context HttpServletResponse response) {
        String json;
        String registerError;
        String page = "/master/production/login.jsp#toregister";
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        Message message;
        Gson gson = new Gson();
        HttpSession session = request.getSession();

        try {
            HttpConfig httpConfig = HttpConfig.custom()
                    .setDefaultEncode("UTF-8")
                    .setRedirectStrategy(true)
                    .setHttps(true)
                    .build();

            paramsMap.put("userName", userName);
            paramsMap.put("password", password);
            paramsMap.put("alias", alias);

            // 调用自己写的接口测试插件中的方法
            IHttpRequest httpRequest = (IHttpRequest) ParamsProxy.newInstance(new HttpRequest(httpConfig), null);
           // 调用测试公共接口(registerUrl=http://xxx.xxx.xx.xx:8081/resources/account/iRegister)
            json = httpRequest.doPost(GlobalParam.registerUrl, paramsMap);
            message = gson.fromJson(json, Message.class);

            if (message.getCode().equals(ResultCode.SUCCESS)) {
                session.setAttribute("userName", userName);
                session.setAttribute("alias", alias);
                page = "/master/production/index.jsp";
            } else {
                registerError = null == message.getDesc() ? "" : message.getDesc();
                logger.info("registerError = " + registerError);
                session.setAttribute("registerError", registerError);
            }
            response.sendRedirect(page);
        } catch (Exception e) {
            logger.error("", e.fillInStackTrace());
        }


↙↙↙阅读原文可查看相关链接,并与作者交流