LoginSignup
1
0

More than 1 year has passed since last update.

Spring boot_APIにてパラメータ名と変数名が異なる場合の実装方針

Posted at

環境

  • フレームワーク:Spring Boot 2.7.1
  • Java 17

実装APIの種類

  • HTTPメソッド=GET
  • HTTPメソッド=POST(※ただし、Content-Typeが「application/x-www-form-urlencoded」であるもの

やりたかったこと

実装上の変数名とAPIのIF上定義されているパラメータ名(リクエスト、レスポンス)を異なる名前にしたい。

本実装では、以下のように命名規則を違うものにしたい。
実装上の変数名:キャメルケース
APIのIF上定義されているパラメータ名:スネークケース

種類 実装上の変数名 APIのIFで定義されているパラメータ名
リクエストパラメータ testId test_id
レスポンスパラメータ testName test_name

実装方法

リクエストパラメータ

以下記事より、解決策2(ConstructorPropertiesを使用する)の方法を用いて実装。
JavaのSpring Bootにおいて、クエリパラメータのキー名と引数名が異なる場合のフォームクラスの書き方

TestRequest.java
package com.example.springboot.request;

import lombok.Getter;
import org.hibernate.validator.constraints.Length;

import java.beans.ConstructorProperties;

@Getter
public class TestRequest {
    @Length(max = 5) // バリデーション
    private String testId;

    @ConstructorProperties({"test_id"})
    public TestRequest(
            String testId
    ) {
        this.testId = testId;
    }
}

レスポンスパラメータ

以下記事内で紹介されているjacksonの「@JsonProperty」を使用。
Jacksonを使ったJavaとJSONの相互変換(アノテーション)

TestResponse.java
package com.example.springboot.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Setter;

@Setter
@AllArgsConstructor
public class TestResponse {
    @JsonProperty("test_name")
    private String testName;
}

コントローラクラス

上記で実装済みの「TestRequest」および「TestResponse」を用いて実装する。
処理内容としてはリクエストとして受け取った「testId」の文字列の末尾に"_testName"という文字列を結合するだけの簡単な処理。

HelloController.java
package com.example.springboot;

import com.example.springboot.request.TestRequest;
import com.example.springboot.response.TestResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {

	@GetMapping("/sample/get")
	public TestResponse get(TestRequest testRequest) {
		return new TestResponse(testRequest.getTestId().concat("_testName"));
	}


	@PostMapping("/sample/post/urlencoded")
	public TestResponse postUrlencoded(@Validated TestRequest testRequest) {
		return new TestResponse(testRequest.getTestId().concat("_testName"));
	}

}

実行結果

想定通りの処理になった。

HTTPメソッド=GET

リクエスト

※実際にはPOSTMANを使ってリクエストを送っているが、curlコマンドで記載する。

curl --location --request GET 'http://localhost:8080/sample/get?test_id=12345'

レスポンス

response.json
{
    "test_name": "12345_testName"
}

HTTPメソッド=POST(※ただし、Content-Typeが「application/x-www-form-urlencoded」

リクエスト

curl --location --request POST 'http://localhost:8080/sample/post/urlencoded' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'test_id=54321'

レスポンス

response.json
{
    "test_name": "54321_testName"
}

補足

なぜ実装方針の内容でやりたいことを実現することができたのか、技術的な内容については別記事で記載する予定。

1
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
1
0