0
0

Query Parameter (GET) と HTML Form (POST) のリクエストパラメータ照会

Last updated at Posted at 2023-12-16
  • @ModelAttribute(最も推薦)、@RequestParam、request.getParameter()を使用してRequestパラメータを照会することができる。

@ModelAttribute

  • オブジェクトを生成し、リクエストパラメータをオブジェクトに自動的に設定してくれる。
  • Typeが合わないとBindExceptionが発生する。
@RequestMapping("/model-attribute") 
// @ModelAttributeは省略できる。
// public String modelAttributeV1(@ModelAttribute HelloData helloData) {
public String modelAttributeV1(HelloData helloData) {
    log.info("username={}, age={}", helloData.getUsername(), helloData.getAge());
    log.info("helloData={}", helloData);
    return "ok";
}

// http://localhost:8080/model-attribute?username=test1&age=10
2023-10-02 09:49:06.758  INFO 73270 --- [nio-8080-exec-1] h.s.b.request.RequestParamController     : username=test1, age=10
2023-10-02 09:49:06.758  INFO 73270 --- [nio-8080-exec-1] h.s.b.request.RequestParamController     : helloData=HelloData(username=test1, age=10)
@Data
public class HelloData {
    private String username;
    private int age; 
}

@RequestParam

  • パラメータ名でBindingする。
@RequestMapping("/request-param-v2")
public String requestParamV2(
        @RequestParam("username") String memberName,
        @RequestParam("age") int memberAge
){
    log.info("username={}, age={}", memberName, memberAge);
    return "ok";
}

// HTTPパラメータ名が変数名と同じであれば(" ")省略可能である。
@RequestMapping("/request-param-v3")
public String requestParamV3(
        @RequestParam String username,
        @RequestParam int age
){
    log.info("username={}, age={}", username, age);
    return "ok";
}

// String、int、Integerなどの単純タイプであれば@RequestParamも省略可能である。
@RequestMapping("/request-param-v4")
public String requestParamV4(
        String username,
        int age
) {
    log.info("username={}, age={}", username, age);
    return "ok";
}

// requiredも設定可能であり、デフォルト値はtrueである。
// デフォルトValueも設定可能である。 defaultValueを使うと、requiredは必要ない。
// defaultValueは空文字の場合にも設定したデフォルト値が適用される。
@RequestMapping("/request-param-required-default")
public String requestParamRequiredDefault(
        @RequestParam(required = true, defaultValue = "guest") String username,
        @RequestParam(required = false, defaultValue = "-1") int age
){
    log.info("username={}, age={}", username, age);
    return "ok";
}

// リクエストパラメータをMapまたはMultiValueMapで照会することもできる。
// ?username=name1&username=2のようにパラメータ値が1つであることが確実でない場合は、
// MultiValueMapを使おう。
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap){
    log.info("username={}, age={}", paramMap.get("username"), paramMap.get("age"));
    return "ok";
}

request.getParameter()

@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");
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0