当使用@RequestParam或@ModelAttribute接收一个请求参数时的行为

简述

    Spring Web MVC の @RequestParam や @ModelAttribute でリクエストパラメータ1つを受け取ったときの挙動を確認するサンプルを示す

验证活动环境

    • Java 11 (AdoptOpenJDK 11.0.6+10)

 

    • Gradle 6.2.2

 

    • Spring Boot 2.2.5

 

    Spring Web MVC 5.2.4

演示控制器的动作确认示例代码 DemoController.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoController {

  public static void main(String[] args) {
    SpringApplication.run(DemoController.class, args);
  }

  @GetMapping("/request-param")
  public String foo(@RequestParam(name = "myParam") String myParam) {
    return "myParam=[" + myParam + "]\n\n";
  }

  @GetMapping("/model-attribute")
  public String bar(@ModelAttribute(name = "myParam") String myParam) {
    return "myParam=[" + myParam + "]\n\n";
  }

  @ExceptionHandler
  public String handleError(Throwable e) {
    return e.toString() + "\n\n";
  }
}

使用Gradle启动Spring Boot

准备一个build.gradle文件。

plugins {
  id 'org.springframework.boot' version '2.2.5.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

group = 'com.example'
version = '0.0.1'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
}

使用gradle bootRun命令启动Spring Boot。

$ gradle bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

使用curl发送请求

准备一个名为a.sh的文件。

curl http://localhost:8080/request-param
curl http://localhost:8080/request-param?myParam
curl http://localhost:8080/request-param?myParam=hello
curl http://localhost:8080/request-param?myParam=%20%20%20
curl http://localhost:8080/request-param?myParam=+++
curl http://localhost:8080/request-param?myParam=%20%20Hello%20World%20%20
curl http://localhost:8080/request-param?myParam=++Hello+World++
curl http://localhost:8080/model-attribute
curl http://localhost:8080/model-attribute?myParam
curl http://localhost:8080/model-attribute?myParam=hello
curl http://localhost:8080/model-attribute?myParam=%20%20%20
curl http://localhost:8080/model-attribute?myParam=+++
curl http://localhost:8080/model-attribute?myParam=%20%20Hello%20World%20%20
curl http://localhost:8080/model-attribute?myParam=++Hello+World++

通过sh -x执行。

$ sh -x a.sh 
+ curl http://localhost:8080/request-param
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'myParam' is not present

+ curl 'http://localhost:8080/request-param?myParam'
myParam=[]

+ curl 'http://localhost:8080/request-param?myParam=hello'
myParam=[hello]

+ curl 'http://localhost:8080/request-param?myParam=%20%20%20'
myParam=[   ]

+ curl 'http://localhost:8080/request-param?myParam=+++'
myParam=[   ]

+ curl 'http://localhost:8080/request-param?myParam=%20%20Hello%20World%20%20'
myParam=[  Hello World  ]

+ curl 'http://localhost:8080/request-param?myParam=++Hello+World++'
myParam=[  Hello World  ]

+ curl http://localhost:8080/model-attribute
myParam=[]

+ curl 'http://localhost:8080/model-attribute?myParam'
myParam=[]

+ curl 'http://localhost:8080/model-attribute?myParam=hello'
myParam=[hello]

+ curl 'http://localhost:8080/model-attribute?myParam=%20%20%20'
myParam=[]

+ curl 'http://localhost:8080/model-attribute?myParam=+++'
myParam=[]

+ curl 'http://localhost:8080/model-attribute?myParam=%20%20Hello%20World%20%20'
myParam=[  Hello World  ]

+ curl 'http://localhost:8080/model-attribute?myParam=++Hello+World++'
myParam=[  Hello World  ]

动作

“关于@RequestParam(name = “myParam”)的行为”

    • パラメータ名が指定されていない場合: MissingServletRequestParameterException 例外が発生

 

    パラメータ値が指定されていない場合: 引数に空文字列がセットされる

@ModelAttribute(name = “myParam”) 的行为如何?

    • パラメータ名が指定されていない場合: 引数に空文字列がセットされる

 

    • パラメータ値が指定されていない場合: 引数に空文字列がセットされる

 

    パラメータ値が半角空白文字のみで構成されている場合: 引数に空文字列がセットされる

参考资料

    • 入門 | Spring MVC で Web コンテンツの提供 – コードサンプル

 

    • RequestParam (Spring Framework 5.2.4.RELEASE API) – Javadoc 日本語訳

 

    • ModelAttribute (Spring Framework 5.2.4.RELEASE API) – Javadoc 日本語訳

 

    • ExceptionHandler (Spring Framework 5.2.4.RELEASE API) – Javadoc 日本語訳

 

    • 7.1.11. エラー処理 – Spring Boot の機能 – 公式ドキュメントの日本語訳

 

    技術/HTTP/URLエンコードで 0x20(スペース) を “+” にすべきか “%20” にすべきか – Glamenv-Septzen.net
bannerAds