새소식

🏫 Framework/🍃 Spring

[Spring] request 안의 모든 parameter 확인하는 방법

728x90

🤔 Question

👉 Spring에서 HttpServletRequest 또는 @RequestParam을 사용하면, request 안의 모든 parameter를 확인할 수 있습니다. 예시를 통해 자세히 알려드리겠습니다.

 

🎯 HttpServletRequest 사용

👉 우선, HttpServletRequest를 이용해 모든 요청 파라미터를 확보해보겠습니다.

import jakarta.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/params")
    public Map<String, String> getParams(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            params.put(paramName, request.getParameter(paramName));
        }
        
        return params;
    }
}

getParameterNames()로 모든 키를 조회한 후, getParameter()로 값을 가져오시면 됩니다.

 

🎯@RequestParam 사용

👉 @RequestParam을 Map<String, String>으로 받아 모든 요청 파라미터를 자동으로 맵핑하는 방법도 있습니다.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/params")
    public Map<String, String> getParams(@RequestParam Map<String, String> params) {
        return params;
    }
}

코드가 간결하고, GET 요청에 최적화되어 있습니다.

 

🎯 @RequestBody를 사용해 JSON 요청 받기

👉 @RequestBody를 사용하여 JSON 형식의 요청을 Map<String, Object>로 받을 수 있습니다.

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MyController {

    @PostMapping("/json")
    public Map<String, Object> getJson(@RequestBody Map<String, Object> body) {
        return body;
    }
}

POST 요청에서 JSON 데이터를 다룰 때 유용합니다.

 

🎯 Enumeration을 이용하여 parameter 확인

👉 request 안의 모든 parameter 확인하는 방법은 다음과 같습니다

Enumeration params = request.getParameterNames();
System.out.println("----------------------------");
while (params.hasMoreElements()){
    String name = (String)params.nextElement();
    System.out.println(name + " : " +request.getParameter(name));
}
System.out.println("----------------------------");

 

결과는 위와 같습니다~!

 

☔ 정리

👉 어떤 방식을 사용할지는 요청 방식과 데이터 형식에 따라 결정하면 됩니다. 🚀

방법 설명 GET 지원 POST 지원
HttpServletRequest 모든 요청 정보를 직접 조회 가능
@RequestParam 간단하게 요청 파라미터를 맵핑 ❌ (폼 데이터는 가능)
@RequestBody JSON 데이터 받기

 

If I was of any help to you, please buy me coffee 😿😢😥

If you have any questions, please leave them in the comments

🧭 References

[1] reference : https://doctorson0309.tistory.com/

[2] Ads : https://play.google.com/store/apps/details?id=io.cordova.seoulfilter

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.