@RestController

기본적으로 Rest 방식의 Controller를 생성할 것이다.

package com.springbootaws.book.springboot.web;

import com.springbootaws.book.springboot.service.posts.PostsService;
import com.springbootaws.book.springboot.web.dto.PostResponseDto;
import com.springbootaws.book.springboot.web.dto.PostSaveRequestDto;
import com.springbootaws.book.springboot.web.dto.PostUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class PostApiController {
    // service모듈을 생성자 DI받을 수 있도록 해서 서비스 로직을 상황에 따라서 다르게할 수 있다.
    private final PostsService postsService;

    @PostMapping("/api/v1/posts")
    public Long save(@RequestBody PostSaveRequestDto postSaveRequestDto){
        return postsService.save(postSaveRequestDto);
    }

    @PutMapping("/api/v1/posts/{id}")
    public Long update(@PathVariable Long id, @RequestBody PostUpdateRequestDto postUpdateRequestDto){
        return postsService.update(id, postUpdateRequestDto);
    }

    @GetMapping("/api/v1/posts/{id}")
    public PostResponseDto findById(@PathVariable Long id){
        return postsService.findById(id);
    }

    @DeleteMapping("/api/v1/posts/{id}")
    public Long deleteById(@PathVariable Long id){
        postsService.delete(id);
        return id;
    }
}
@Controller
public class PostApiController{
		private final PostsService postsService;
	
    @GetMapping("/api/v1/posts/{id}")
    public @ResponseBody PostResponseDto findById(@PathVariable Long id){
        return postsService.findById(id);
    }
}

@RestController
public class PostApiController{
		private final PostsService postsService;
	
    @GetMapping("/api/v1/posts/{id}")
    public PostResponseDto findById(@PathVariable Long id){
        return postsService.findById(id);
    }
}

HTTP 요청 매핑

    @PostMapping("/api/v1/posts")
    public Long save(@RequestBody PostSaveRequestDto postSaveRequestDto){
        return postsService.save(postSaveRequestDto);
    }

    @PutMapping("/api/v1/posts/{id}")
    public Long update(@PathVariable Long id, @RequestBody PostUpdateRequestDto postUpdateRequestDto){
        return postsService.update(id, postUpdateRequestDto);
    }

    @GetMapping("/api/v1/posts/{id}")
    public PostResponseDto findById(@PathVariable Long id){
        return postsService.findById(id);
    }

    @DeleteMapping("/api/v1/posts/{id}")
    public Long deleteById(@PathVariable Long id){
        postsService.delete(id);
        return id;
    }

요청으로부터 필요한 데이터 가져오기

요청의 body에서 필요한 데이터 가져오기 ( @RequestBody )

// @RequestBody {변수 타입} {변수 이름} 으로 body의 데이터를 가져올 수 있다.
public Long save(@RequestBody PostSaveRequestDto postSaveRequestDto){
    return postsService.save(postSaveRequestDto);
}

요청의 queryString에서 필요한 데이터 가져오기 ( @RequestParam )

// @RequestParam {변수 타입} {변수 이름} 요청의 쿼리 스트링에서 데이터를 가져온다.
// 요청 매핑 시 path에 따로 작성할 필요 없다.
@GetMapping("/api/v1/posts")
public PostResponseDto findById(@RequestParam Long id){
    return postsService.findById(id);
}

요청의 파라미터 가져오기 ( @PathVariable )

// @PathVariable {변수 타입} {변수 이름} 으로 url의 파라미터를 가져올 수 있다.
// 요청 매핑 시 path에 {변수 이름}의 형태로 명시해야 한다.
@GetMapping("/api/v1/posts/{id}")
public PostResponseDto findById(@PathVariable Long id){
    return postsService.findById(id);
}

@Controller