Spring MVC에서 쓰이는 Annotation@Controller
@ResponseBody
@RestController
@Controller + @ResponseBody@RequestMapping("/api/members")
method = RequestMethod.POST
@GetMapping("/hello")
@PostMapping("/bye")
@~Mapping은 @RequestMapping을 감싼 어노테이션@ResponseBody
@GetMapping("/{id}")
public String getMemberProfile(@PathVariable Long id) {
return "요청하신 " + id + "번 회원의 프로필을 조회 중입니다...";
}
@PathVariable이 주소창의 {id} 값을 낚채서 변수 id에 담아줍니다./hello/10이면 id = 10int id가 있어서 이미 변수로 사용된다면 @PathVariable("id") Long no로 id임을 ("id")로 표기하고 다르게 사용가능@GetMapping("/search")
public String searchMember(String name, @RequestParam(required = false, defaultValue = "10") int age) {
return name + "이라는 이름을 가진 회원들을 검색 중입니다..." + age + "세이시군요!";
}
@RequestParam이 url의 ?name= 뒤에 있는 값을 name 변수에 담아줍니다.@RequestParam(required = false, defaultValue = "10") int age)required = false: 필수는 아님defaultValue = "10": 디폴트 값이 10| 구분 | 사용처 |
|---|---|
| PathVariable | 경로 일부 |
| RequestParam | 쿼리 스트링 |
@PostMapping("/signup")
public String signup(@RequestBody MemberSignupRequest request) {
System.out.println("가입 요청 이메일: " + request.getEmail());
return request.getNickname() + "님, 가입 신청이 완료되었습니다!";
}
@RequestBody가 HTTP 요청 Body(JSON 등)를 자바 객체(Request DTO)로 변환해줍니다.