728x90
이 어노테이션들은 controller에서 프론트로부터 파라미터를 전달받을 때 사용하는 어노테이션들이다.
@GetMapping("/food")
public FoodResponseDto findById(@RequestParam(value = "id") Long id) {
return foodService.findById(id);
}
api 요청 시에 URI에 http://localhost:8080/food?id=2 이렇게 queryString 방식으로 넣어주는 것이다.
@RequestParam(value = "id" , required=false) 이렇게 쓰면 id값을 넣어주지 않아도 오류가 발생하지 않는다.
2. @PathVariable
@GetMapping("/api/food/menu/{id}")
public FoodResponseDto findById(@PathVariable Long id) {
return foodService.findById(id);
}
처럼 api 요청 시에 URI에 http://localhost:8080/api/udong/club/2 이런 식으로 값을 넣어 id 값 = 2 라는 정보를 전달해주는 것이다.
3. @RequestBody
@PostMapping("/api/food/menu")
public Long save(@RequestBody FoodSaveRequestDto requestDto) {
return foodService.save(requestDto);
}
예를들어 프론트에서
{
"id":2,
"name":"김밥"
}
이런 json 형식의 데이터를 서버로 보내주면 이를 받아서 자바 객체 형태로 매핑해준다.
@PathVariable 추가
Ex) 비회원, 회원 관리자 구분을 PathVariable을 통해서 함! (담당자, 비담당자 임원 비임원)
package com.jth.exercise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test")
public class TestController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
@GetMapping("/list")
public String testList() {
LOGGER.info("TestController,testList.");
return "test/testList";
}
@GetMapping("/detail/{no}")
public String testDetail(@PathVariable("no") int no, Model model) {
LOGGER.info("TestController,testDetail.");
LOGGER.info("check no : {}", no);
model.addAttribute("no",no);
return "test/testDetail";
}
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public String testInsert() {
LOGGER.info("TestController,testInsert.");
return "test/testInsert";
}
@PostMapping(value = "/update")
public String testUpdate() {
LOGGER.info("TestController,testUpdate.");
return "test/testUpdate";
}
@PostMapping(value = "/delete")
public String testDelete() {
LOGGER.info("TestController,testDelete.");
return "test/testDelete";
}
}
728x90
'스프링Spring' 카테고리의 다른 글
[Spring] 스프링 model.addAttribute() 메소드 (0) | 2022.12.02 |
---|---|
[Spring]스프링Spring model 모델 (0) | 2022.12.02 |
[Spring]스프링Spring 라이브러리 (0) | 2022.11.29 |
[Spring] Spring스프링 현업에서 많이쓰는 form/ (0) | 2022.11.28 |
[Spring]스프링Spring 한글 처리 하는 방법 (0) | 2022.11.26 |