每日面经分享(SpringBoot part4:Controller层)

  1. SpringBoot Controller层的作用
    a. 请求映射:Controller层使用注解(如@RequestMapping、@GetMapping、@PostMapping等)将HTTP请求映射到相应的方法上。这些方法根据URL路径、请求方法、请求参数等来决定要执行的操作。
    b. 参数解析:Controller层负责解析HTTP请求中的参数(如路径参数、查询参数、表单参数、请求体等),并将它们传递给相应的方法。Spring Boot提供了各种注解(如@PathVariable、@RequestParam、@RequestBody等)来方便参数的解析和绑定。
    c. 业务逻辑调用:一旦接收到HTTP请求和相关参数,Controller层会调用相应的Service层方法来处理业务逻辑。将传递参数给Service层,并接收返回值。Controller层可以将业务逻辑与用户界面解耦,实现代码的模块化和重用。
    d. 数据封装和返回:Controller层负责将业务逻辑处理的结果封装成适当的数据结构,并通过HTTP响应返回给客户端。这可以是JSON、XML、HTML等形式的数据。Spring Boot提供了各种注解(如@ResponseBody、@RestController等)来方便数据的封装和返回。
    e. 异常处理:Controller层也负责处理异常情况。使用try-catch块来捕获异常,并根据需要进行适当的处理(如返回错误信息、重定向等)。Spring Boot还提供了@ExceptionHandler注解来定义全局异常处理方法,以统一处理应用程序中的异常。
  2. SpringBoot Controller层搭建过程
    a. 创建Controller类:创建一个名为UserController的类,并添加@RestController注解进行标识。
    b. 依赖注入Service层:在Controller类中,通过构造函数注入UserService实例。这样可以在Controller中使用Service层提供的业务逻辑功能。
    c. 处理HTTP请求:使用各种注解(如@GetMapping、@PostMapping、@PutMapping、@DeleteMapping等)来处理不同类型的HTTP请求。根据请求的URL路径和参数,调用相应的方法来处理请求。
    d. 参数解析:在方法参数中使用注解(如@PathVariable、@RequestBody等)来解析HTTP请求中的参数。这些注解可以将路径参数、请求体等绑定到方法参数上,方便后续处理。
    e. 业务逻辑处理:在方法中调用UserService中的方法来处理业务逻辑。根据需要对数据进行处理和转换,以满足应用程序的需求。
    f. 数据封装和返回:根据业务逻辑处理的结果,将数据封装成适当的数据结构,并通过方法的返回值进行返回。使用ResponseEntity来包装返回结果,可以设置响应状态码和响应头等信息。
@RestController
@RequestMapping("/users")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUserById(@PathVariable Long id) {
        UserDTO user = userService.getUserById(id);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/")
    public ResponseEntity<List<UserDTO>> getAllUsers() {
        List<UserDTO> users = userService.getAllUsers();
        if (!users.isEmpty()) {
            return ResponseEntity.ok(users);
        } else {
            return ResponseEntity.noContent().build();
        }
    }

    @PostMapping("/")
    public ResponseEntity<Void> saveUser(@RequestBody CreateUserRequest request) {
        Long userId = userService.saveUser(request);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(userId)
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateUser(@PathVariable Long id, @RequestBody UpdateUserRequest request) {
        userService.updateUser(id, request);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }
}

互联网大厂测开经历,目前担任测试开发负责人,每天分享互联网面经,如果你有测试相关的问题,欢迎咨询,海鲜市场【简历优化】、【就业指导】、【模拟/辅导面试】,已辅导20位以上同学拿到心仪offer

海鲜市场

相关推荐

  1. (24-4

    2024-04-01 13:42:03       13 阅读
  2. 4-10

    2024-04-01 13:42:03       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 13:42:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 13:42:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 13:42:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 13:42:03       18 阅读

热门阅读

  1. Android笔记--MediaCodec(一)

    2024-04-01 13:42:03       14 阅读
  2. 英国生物数据库的申请流程

    2024-04-01 13:42:03       14 阅读
  3. flask+uwsgi+云服务器 部署服务端

    2024-04-01 13:42:03       22 阅读
  4. 【微服务篇】分布式事务方案以及原理详解

    2024-04-01 13:42:03       16 阅读
  5. 多线程(24)Future接口

    2024-04-01 13:42:03       15 阅读
  6. 设计模式之策略模式

    2024-04-01 13:42:03       12 阅读