신규 클래스 등록 후
@MapController 와 @RespnseBody를 이용한 Response Test
package com.std.sbb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// @Controller
@Controller
public class HomeController {
//@getMapping("home/main")
// to springboot. if there is /home/main request. call up the method
@GetMapping("home/main")
@ResponseBody // resonse whitout jsp, html etc (formal response material)
public String ShowMain(){
return "Greetings.";
}
}
Spring Boot에서 @RequestMapping 어노테이션은 웹 요청을 특정 핸들러 메소드에 매핑하는 데 사용. 즉, 특정 URL 경로로 들어오는 HTTP 요청을 처리할 메소드를 지정하는 역할. @RequestMapping은 클래스 레벨 또는 메소드 레벨에 모두 적용될 수 있으며, 이를 통해 컨트롤러에서 다양한 요청을 처리할 수 있다.
@RequestMapping 어노테이션은 URL 경로를 지정하여 해당 경로로 들어오는 요청을 처리할 메소드를 연결합니다. 예를 들어, @RequestMapping("/users")는 "/users" 경로로 들어오는 모든 요청을 처리하는 메소드에 적용될 수 있다.
@RequestMapping은 GET, POST, PUT, DELETE 등 다양한 HTTP 메서드를 지원. @RequestMapping 어노테이션의 method 속성을 사용하여 특정 HTTP 메서드만 매핑할 수도 있다.
@RequestMapping을 클래스 레벨에 적용하면, 해당 클래스 내의 모든 메소드에 공통적인 URL 경로 접두사를 설정할 수 있습니다. 예를 들어, @RequestMapping("/api")가 클래스 레벨에 적용되면, 해당 클래스 내의 모든 메소드는 "/api" 경로를 접두사로 갖게 됩니다.
@RequestMapping을 메소드 레벨에 적용하면, 특정 URL 경로 및 HTTP 메서드에 대한 매핑을 설정할 수 있습니다. 예를 들어, @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)는 "/users/{id}" 경로로 들어오는 GET 요청을 처리하는 메소드에 적용.
@RequestMapping은 value 속성을 사용하여 URL 경로를 지정하고, method 속성을 사용하여 HTTP 메서드를 지정하는 간단한 사용법을 제공합니다.
@RequestMapping은 URL 경로, HTTP 메서드, 요청 매개변수, 헤더 등을 기반으로 유연하게 요청을 매핑할 수 있도록 지원합니다.
@RequestMapping은 컨트롤러에서 사용될 때, 반환값이 View 이름을 의미할 수 있지만, @RestController는 Rest API 요청에 대한 응답 (JSON 등)을 반환합니다.
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getUser(@PathVariable Long id, Model model) {
// 사용자 ID를 기반으로 사용자 정보를 조회하여 모델에 추가
model.addAttribute("user", userService.getUser(id));
return "user-details"; // user-details.html 뷰를 렌더링
}
@RequestMapping(method = RequestMethod.POST)
public String createUser(@ModelAttribute User user) {
// 사용자 정보를 저장
userService.createUser(user);
return "redirect:/users"; // 사용자 목록 페이지로 리다이렉트
}
}
위 예시에서 @RequestMapping("/users")는 클래스 레벨에 적용되어 모든 메소드에 "/users" 경로를 접두사로 추가. @RequestMapping(value = "/{id}", method = RequestMethod.GET)은 "/users/{id}" 경로로 들어오는 GET 요청을 처리하는 메소드에 적용됩니다. @RequestMapping(method = RequestMethod.POST)는 "/users" 경로로 들어오는 POST 요청을 처리하는 메소드에 적용됩니다.
@RequestMapping은 Spring Boot에서 HTTP 요청을 특정 핸들러 메소드에 매핑하는 데 사용되는 어노테이션입니다. URL 경로, HTTP 메서드, 요청 매개변수, 헤더 등을 기반으로 유연하게 요청을 매핑할 수 있으며, 컨트롤러에서 다양한 요청을 처리할 수 있도록 지원합니다.