Spring MVC Controller:太多(胖)了很多?

时间:2017-06-19 08:08:35

标签: java spring-mvc

我不太确定在这里问这样的问题是否可以,但是我的Spring MVC控制器遇到了问题,我觉得它太胖了。

以下是一个样本:

@GetMapping("/firms/firm/{id}")
    public String getFirmById (@PathVariable String id,
                               Map <String, Object> model,
                               HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse,
                               HttpSession httpSession) throws IOException {

        log.info("Getting data for firm with id {}", id);
        model.put(VERSION, applicationVersion);
        Cookie cookie = authService.findCookie(servletRequest, servletResponse);
        HttpHeaders httpHeaders = authService.createJwtAuthHeader(cookie);
        HttpEntity requestEntity = new HttpEntity(httpHeaders);
        ResponseEntity <UserObject> userObjectResponse = authService.createUserResponseEntity(requestEntity, servletResponse);
        authService.setUserSessionDetails(userObjectResponse, httpSession);

        if (userObjectResponse != null && userObjectResponse.getBody() != null) {
            log.info(CommonMessages.GOT_COOKIE_FROM_AUTH_SERVICE.toString(), cookie.getName());
            servletResponse.setStatus(HttpServletResponse.SC_OK);
            ResponseEntity <Firm> firmResponse = restTemplate.exchange(urlApiFirms + id,
                    HttpMethod.GET,
                    requestEntity,
                    Firm.class);
            model.put("firm", firmResponse.getBody());
            return "firm";
        } else {
            log.error(CommonMessages.NO_COOKIES_FOUND_NO_ACCESS_REDIRECTING.toString());
            servletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            httpSession.setAttribute("authorized", false);
            return "error";
        }
    }

每个控制器都以安全检查开始,这是此应用程序的要求。我以为我可以将代码从Cookie cookie = authService.findCookie(servletRequest, servletResponse);开始直到并包含authService.setUserSessionDetails(userObjectResponse, httpSession);到一个单独的服务中,以便删除重复的代码。

但是我仍然需要为Thymeleaf model填写价值观,我也可以从其他服务处获得。但这会让我的控制器调用更多的服务,我不确定这是正确的方法。

我是按照自己的想法进入正确的方向还是完全离开? 谢谢!

0 个答案:

没有答案