@Controller
주로 View를 반환하기 위해 사용함
@ResponseBody
컨트롤러에서 데이터를 반환할때 쓴다.
//@ResponseBody예제
@Controller
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@GetMapping(value = "/users")
public @ResponseBody ResponseEntity<User> findUser(@RequestParam("userName") String userName){
return ResponseEntity.ok(userService.findUser(user));
}
@GetMapping(value = "/users/detailView")
public String detailView(Model model, @RequestParam("userName") String userName){
User user = userService.findUser(userName);
model.addAttribute("user", user);
return "/users/detailView";
}
}
@RestController
@Controller에 @ResponseBody가 추가된 것이다.
RestController의 주용도는 Json 형태로 객체 데이터를 반환하는것
최근엔 데이터를 응답으로 제공하는 REST API 개발할 때 주로 사용하고, ResponseEntity로 감싸서 반환한다.
package com.sample.coffee.controller;
import com.codestates.coffee.dto.CoffeePatchDto;
import com.codestates.coffee.dto.CoffeePostDto;
import com.codestates.coffee.dto.CoffeeResponseDto;
import com.codestates.coffee.entity.Coffee;
import com.codestates.coffee.mapper.CoffeeMapper;
import com.codestates.coffee.service.CoffeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/v5/coffees")
@Validated
public class CoffeeController {
private CoffeeService coffeeService;
private CoffeeMapper mapper;
public CoffeeController(CoffeeService coffeeService, CoffeeMapper coffeeMapper) {
this.coffeeService = coffeeService;
this.mapper = coffeeMapper;
}
@PostMapping
public ResponseEntity postCoffee(@Valid @RequestBody CoffeePostDto coffeePostDto) {
Coffee coffee = coffeeService.createCoffee(mapper.coffeePostDtoToCoffee(coffeePostDto));
return new ResponseEntity<>(mapper.coffeeToCoffeeResponseDto(coffee), HttpStatus.CREATED);
}
@PatchMapping("/{coffee-id}")
public ResponseEntity patchCoffee(@PathVariable("coffee-id") @Positive long coffeeId,
@Valid @RequestBody CoffeePatchDto coffeePatchDto) {
coffeePatchDto.setCoffeeId(coffeeId);
Coffee coffee = coffeeService.updateCoffee(mapper.coffeePatchDtoToCoffee(coffeePatchDto));
return new ResponseEntity<>(mapper.coffeeToCoffeeResponseDto(coffee), HttpStatus.OK);
}
@GetMapping("/{coffee-id}")
public ResponseEntity getCoffee(@PathVariable("coffee-id") long coffeeId) {
Coffee coffee = coffeeService.findCoffee(coffeeId);
return new ResponseEntity<>(mapper.coffeeToCoffeeResponseDto(coffee), HttpStatus.OK);
}
@GetMapping
public ResponseEntity getCoffees() {
List<Coffee> coffees = coffeeService.findCoffees();
List<CoffeeResponseDto> response = mapper.coffeesToCoffeeResponseDtos(coffees);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@DeleteMapping("/{coffee-id}")
public ResponseEntity deleteCoffee(@PathVariable("coffee-id") long coffeeId) {
coffeeService.deleteCoffee(coffeeId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
RequestMapping
: 메소드들과 요청을 매핑할 때 사용한다.PathVariable
: REST API에서 URI에 변수가 들어가는걸 실무에서 많이 볼 수 있다.예를 들면, 아래 URI에서/1234,/4062464가 @PathVariable로 처리해줄 수 있는 부분이다.