title: Difference between service layer and controller in practice
body: <p>I have read many theories about differences between service layer and controller, and I have some questions about how to realize this in practice. One answer to <a href="https://stackoverflow.com/questions/3885675/service-layer-and-controller-who-takes-care-of-what">Service layer and controller: who takes care of what?</a> says: </p>
  
  <blockquote>
    <p>I try to restrict controllers to doing work related to validating http
    parameters, deciding what service method to call with what parameters,
    what to put in the httpsession or request, what view to redirect or
    forward to, or similar web-related stuff.</p>
  </blockquote>
  
  <p>and from <a href="http://www.bennadel.com/blog/2379-a-better-understanding-of-mvc-model-view-controller-thanks-to-steven-neiland.htm" rel="noreferrer">http://www.bennadel.com/blog/2379-a-better-understanding-of-mvc-model-view-controller-thanks-to-steven-neiland.htm</a> :</p>
  
  <blockquote>
    <p>Red Flags: My Controller architecture might be going bad if:</p>
    
    <p>The Controller makes too many requests to the Service layer. The
    Controller makes a number of requests to the Service layer that don't
    return data. The Controller makes requests to the Service layer
    without passing in arguments.</p>
  </blockquote>
  
  <p>At the moment I am developing a web app with Spring MVC, and I have such method for saving changed user's email:</p>
  
  <pre><code>/**
       * &lt;p&gt;If no errors exist, current password is right and new email is unique,
       * updates user's email and redirects to {@link #profile(Principal)}
       */
      @RequestMapping(value = "/saveEmail",method = RequestMethod.POST)
      public ModelAndView saveEmail(
              @Valid @ModelAttribute("changeEmailBean") ChangeEmailBean changeEmailBean,
              BindingResult changeEmailResult,
              Principal user,
              HttpServletRequest request){
  
          if(changeEmailResult.hasErrors()){
              ModelAndView model = new ModelAndView("/client/editEmail");
              return model;
          }
          final String oldEmail = user.getName();
          Client client = (Client) clientService.getUserByEmail(oldEmail);
          if(!clientService.isPasswordRight(changeEmailBean.getCurrentPassword(), 
                                            client.getPassword())){
              ModelAndView model = new ModelAndView("/client/editEmail");
              model.addObject("wrongPassword","Password doesn't match to real");
              return model;
          }
          final String newEmail = changeEmailBean.getNewEmail();
          if(clientService.isEmailChanged(oldEmail, newEmail)){
              if(clientService.isEmailUnique(newEmail)){
                  clientService.editUserEmail(oldEmail, newEmail);
                  refreshUsername(newEmail);
                  ModelAndView profile = new ModelAndView("redirect:/client/profile");
                  return profile;
              }else{
                  ModelAndView model = new ModelAndView("/client/editEmail");
                  model.addObject("email", oldEmail);
                  model.addObject("emailExists","Such email is registered in system already");
                  return model;
              }
          }
          ModelAndView profile = new ModelAndView("redirect:/client/profile");
          return profile;
      }
  </code></pre>
  
  <p>You can see that I have a lot of requests to the service layer, and I do redirecting from controller - that is business logic. Please show better version of this method.</p>
  
  <p>And another example. I have this method, which returns user's profile:</p>
  
  <pre><code>/**
       * Returns {@link ModelAndView} client's profile
       * @param user - principal, from whom we get {@code Client}
       * @throws UnsupportedEncodingException
       */
      @RequestMapping(value = "/profile", method = RequestMethod.GET)
      public ModelAndView profile(Principal user) throws UnsupportedEncodingException{
          Client clientFromDB = (Client)clientService.getUserByEmail(user.getName());
          ModelAndView model = new ModelAndView("/client/profile");
          model.addObject("client", clientFromDB);
          if(clientFromDB.getAvatar() != null){
              model.addObject("image", convertAvaForRendering(clientFromDB.getAvatar()));
          }
          return model;
      }
  </code></pre>
  
  <p>method convertAvaForRendering(clientFromDB.getAvatar()) is placed in super class of this controller, it is right placing of this method, or he must be placed in service layer??</p>
  
  <p>Help please, it is really important for me.</p>