什么属于Controller层,服务层是什么?

时间:2017-03-21 12:25:34

标签: java spring rest spring-boot spring-rest

我有一个简单的Spring-Boot Restful Application。我有一个Controller层和一个Repository层,但没有Service层。让我告诉你一个我的控制器方法:

@RequestMapping(value = "users/{id}", method = RequestMethod.GET)
public Resource<UserResource> get(@PathVariable Long id) throws NotFoundException {
    log.info("Invoked method: get with ID: " + id);
    log.warn("Searching for user with ID " + id);
    User user = userRepository.findOne(id);
    if (user == null){
        log.error("Unexpected error, User with ID " + id + " not found");
        throw new NotFoundException("User with ID " + id + " not found");
    }
    log.info("User found. Sending request back. ID of user is " + id);
    return new Resource<UserResource>(getUserResource(user));
}

由于我没有服务层,我的Controller为我做了业务逻辑。现在我想实现服务层。我的控制器应该/不应该做什么?

我的服务层(我现在想要实现)是否应该完成所有工作,而我的Controller只将请求委托给服务层?

3 个答案:

答案 0 :(得分:2)

服务类

 public class UserService{

 public User findUser(String id){
      log.info("Invoked method: get with ID: " + id);
      log.warn("Searching for user with ID " + id);
      User user = userRepository.findOne(id);
      if (user == null){
        log.error("Unexpected error, User with ID " + id + " not found");
        throw new NotFoundException("User with ID " + id + " not found");
      }
     log.info("User found. Sending request back. ID of user is " + id);
     return user;
  }

}

API类

@RequestMapping(value = "users/{id}", method = RequestMethod.GET)
public Resource<UserResource> get(@PathVariable Long id) throws    NotFoundException {
      return new Resource<UserResource>(userService.findUser(id));
 }
}

添加常见异常处理程序NotFoundException以重定向到正确的错误页面。

答案 1 :(得分:2)

问问自己:如果我想为不同的视图/传输/协议呈现结果,需要更改什么?这属于控制器。

Controller层中的代码只应与Service层与视图/传输/协议(视情况而定)之间的业务输入/输出映射相关。这可能(或可能不包括)将业务数据映射到JSON(您的业务/服务层直接使用JSON或类似工具),XML,HTML或任何您的内容类型(对于HTTP)都是不合理的。

虽然您的控制器可能感觉轻巧,但请记住,Spring对控制器的支持可以完成大部分工作 - 将这种“简单”控制器视为您的框架识别并挂起所有较重的锅炉的锚点 - 为了你的利益,为了你的利益而编码。

答案 2 :(得分:1)

 public class UserService{

     public User findUser(String id){
          log.info("Invoked method: get with ID: " + id);
          log.warn("Searching for user with ID " + id);
          User user = userRepository.findOne(id); 
          log.info("User found. Sending request back. ID of user is " + id);
          return user;
      }
    }




@RequestMapping(value = "users/{id}", method = RequestMethod.GET)
public Resource<UserResource> get(@PathVariable Long id) throws NotFoundException {
      User user = userService.findUser(id)
      if (user == null){
           log.error("Unexpected error, User with ID " + id + " not found");
           throw new NotFoundException("User with ID " + id + " not found");
      }
      return new Resource<UserResource>(getUserResource(user));
 }
}

基于@Jai响应,唯一的区别是if,因为有时你需要接收一个用户null

public void anotherMethod(@PathVariable Long id){
    User user = userService.findOne(id);
    if(user == null) {
        //in this case I don't want to throw NotFoundException and make some other logic like create the user for example.
    }
}