본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA/Spring

Annotation - @RestController

@RestController
@Controller 어노테이션과 @ResponseBody 어노테이션을 합쳐 놓은 것
컨트롤러 클래스 내의 모든 메서드가 @ResponseBody 어노테이션을 생략하고 반환
RESTful 웹 서비스에서 JSON, XML과 같은 데이터 타입을 반환하고자 할 때 유용하게 사용

@Controller
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users/{id}")
    @ResponseBody
    public User getUser(@PathVariable Long id) {
        User user = userRepository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
        return user;
    }

}
@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable Long id) {
        User user = userRepository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
        return user;
    }

'JAVA > Spring' 카테고리의 다른 글

Spring 버전별 특징  (0) 2023.05.10
RestTemplate이란?  (0) 2023.05.10
Annotation - @RequestBody @ResponseBody  (0) 2023.05.10
Annotation - @ResponseEntity  (0) 2023.05.10
Annotation - @RequiredArgsConstructor  (0) 2023.04.20