spring mvc : @RequestParam
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package com.example.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.RestController;
|
||||
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
|
||||
@RestController
|
||||
@Controller
|
||||
public class RequestParamController {
|
||||
|
||||
@RequestMapping("/request-param-v1")
|
||||
@@ -20,4 +23,56 @@ public class RequestParamController {
|
||||
|
||||
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(defaultValue = "guest") String username,
|
||||
@RequestParam(defaultValue = "-1") int 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";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user