Web系開発メモ

Java, C#, HTML, CSS, JavaScript のことなどを書いてます。

SpringBoot リクエストパラメーターを使う方法

Spring Boot の Webアプリで、リクエストパラメーターを受信して使用する方法を書いていきます。

バージョン

動作確認で使用した製品のバージョンは以下の通りです。

1. リクエストパラメーターの使用方法

パラメーターを引数で受け取って、使用することができます。

src/main/java/org/example/controller/PersonController.java

package org.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@RestController
public class PersonController {
  @Getter @Setter
  @AllArgsConstructor @NoArgsConstructor
  static class Person {
    private long id;
    private String name;
    private int age;
  }
  @GetMapping("/person")
  public Person get(String name, int age) {
    Person person = new Person(1, name, age);
    return person;
  }
  @GetMapping("/person-ant")
  public Person getAnt(
    @RequestParam String name, @RequestParam int age
  ) {
    Person person = new Person(2, name, age);
    return person;
  }
  @GetMapping("/person-obj")
  public Person getObj(Person person) {
    person.setId(3);
    return person;
  }
  @PostMapping("/person")
  public Person post(String name, int age) {
    Person person = new Person(4, name, age);
    return person;
  }
  @PostMapping("/person-ant")
  public Person postAnt(
    @RequestParam String name, @RequestParam int age
  ) {
    Person person = new Person(5, name, age);
    return person;
  }
  @PostMapping("/person-obj")
  public Person postObj(Person person) {
    person.setId(6);
    return person;
  }
}

引数名とパラメーター名を合わせると、Springが値を渡してくれます。アノテーション@RequestParam を使用することもできます。

また、オブジェクトのプロパティ名とパラメーター名を合わせると、引数のオブジェクトに値をバインドすることができます。

2. 動作確認

curl で動作確認をした結果は以下の通りです。

> curl "http://localhost:8080/person?name=John&age=30"
{"id":1,"name":"Jhon","age":30}
> curl "http://localhost:8080/person-ant?name=John&age=30"
{"id":2,"name":"Jhon","age":30}
> curl "http://localhost:8080/person-obj?name=John&age=30"
{"id":3,"name":"Jhon","age":30}

> curl http://localhost:8080/person -d "name=John&age=30"
{"id":4,"name":"Jhon","age":30}
> curl http://localhost:8080/person-ant -d "name=John&age=30"
{"id":5,"name":"Jhon","age":30}
> curl http://localhost:8080/person-obj -d "name=John&age=30"
{"id":6,"name":"Jhon","age":30}

3. 動作確認用資源

動作確認のために、以下のアプリ起動クラスを作成しました。

src/main/java/org/example/SpringApp.java

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Maven のビルドファイルは以下の通りです。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.1</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>org.example</groupId>
  <artifactId>spring-use-param</artifactId>
  <version>1.0.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>17</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>