[Spring] 요청 파라미터 > @RequestParam
- 📚 Spring/SpringMVC
- 2021. 10. 29. 00:34
목표 : 요청 파라미터 > @RequestParam 사용방법 및 활용 예제
package hello.springmvc.basic.request;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@Slf4j
@Controller
public class RequestParamController {
@RequestMapping("/request-param-v1")
public void requestParamV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
log.info("username={}, age={}", username, age);
response.getWriter().write("ok");
}
@ResponseBody
@RequestMapping("/request-param-v2")
public String requestParamV2(
@RequestParam("username") String memberName,
@RequestParam("age") int memberAge) {
log.info("username={}, age={}", memberName, memberAge);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(
@RequestParam String username,
@RequestParam int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(
@RequestParam(required = true) String username,
@RequestParam(required = false) Integer age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(
@RequestParam(required = true, defaultValue = "guest") String username,
@RequestParam(required = false, defaultValue = "-1") Integer age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
//
log.info("username={}, age={}", paramMap.get("username"), paramMap.get("age"));
return "ok";
}
}
'📚 Spring > SpringMVC' 카테고리의 다른 글
HTTP 요청 메시지 - 단순텍스트 (@RequestBody,HttpEntity, RequestEntity) (0) | 2021.10.29 |
---|---|
[Sprng] 요청파라미터 > @ModelAttribute (0) | 2021.10.29 |
[Spring] consumes 와 produces의 차이 (0) | 2021.10.26 |
[Servlet] HTTP 응답 데이터 - API JSON (0) | 2021.10.11 |
[Servlet] HTTP 요청 데이터 - API 메시지 바디 (JSON) (0) | 2021.10.11 |