[TIL] #6. 스프링 MVC - 기본 기능 ②

kiteB·2021년 9월 16일
0

TIL-Spring3

목록 보기
5/7
post-thumbnail

HTTP 요청 파라미터 - 쿼리 파라미터, HTML Form

💡 HTTP 요청 메시지를 통해 서버로 요청 데이터 전달 방법

1. GET - 쿼리 파라미터
: 메시지 바디 없이, URL 쿼리 파라미터에 데이터를 포함해서 전달하는 방식
2. POST - HTML Form
: 메시지 바디에 쿼리 파라미터 형식으로 전달하는 방식
3. HTTP message body에 데이터를 직접 담아서 요청
: HTTP API에서 주로 사용하며, 데이터 형식은 주로 JSON 사용!


요청 파라미터 - 쿼리 파라미터, HTML Form

HttpServletRequestrequest.getParameter()를 사용하면 GET 쿼리 파라미터 전송 방식과 POST HTML Form 방식을 사용할 수 있다.

GET 쿼리 파라미터 전송 방식과 POST HTML Form 방식 모두 형식이 같아서 구분없이 사용할 수 있다. 이를 요청 파라미터(request paramter) 조회라고 한다!


RequestParamController

package hello.springmvc.basic.requestmapping.request;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
public class RequestParamController {

    @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");
    }
}
  • 단순히 HttpServletRequest가 제공하는 방식으로 요청 파라미터를 조회했다.

실행 결과


hello-form.html - Post Form 페이지

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/request-param-v1" method="post">
    username:   <input type="text" name="username" />
    age:        <input type="text" name="age" />
    <button type="submit">전송</button>
</form>
</body>
</html> 

실행 결과


HTTP 요청 파라미터 - @RequestParam

스프링이 제공하는 @RequestParam 애노테이션을 사용하면 getParameter 메서드를 사용하는 방법보다 더 간편하게 사용할 수 있다!

1. requestParamV2

@ResponseBody
@RequestMapping("/request-param-v2")
public String requestParamV2(
    @RequestParam("username") String memberName,
    @RequestParam("age") int memberAge) {

    log.info("username={}, age={}", memberName, memberAge);
    return "ok";
}
  • @RequestParam("username"): username으로 바인딩
  • @ResponseBody: View 조회를 무시하고, HTTP message body에 직접 해당 내용을 입력한다.


2. requestParamV3


@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(
    @RequestParam String username,
    @RequestParam int age) {

    log.info("username={}, age={}", username, age);
    return "ok";
}

HTTP 파라미터 이름이 변수 이름과 같으면 @RequestParam(name="XX") 생략 가능하다!


3. requestParamV4

@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age) {

    log.info("username={}, age={}", username, age);
    return "ok";
}
  • String, int, Integer 등의 단순 타입이면 @RequestParam도 생략이 가능하다.
  • 그래도 웬만하면 @RequestParam 애노테이션 유지가 나중에 알아보기도 쉬우니까 이렇게까지 생략하는 것은 (강사님은) 권장하지 않는다😣


4. requestParamRequired

@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(
    @RequestParam(required = true) String username,
    @RequestParam(required = false) Integer age) {

    log.info("username={}, age={}", username, age);
    return "ok";
}
  • @RequestParam.required
    • 파라미터 필수 여부
    • 기본값은 true(파라미터 필수)
  • usernamerequired = true
    • username없으면 에러가 발생한다.
    • /request-param?username= 처럼 파라미터 이름만 있고 값이 없는 경우 빈 문자로 전달된다!
  • agerequired = false
    • requiredfalse이지만, int형은 null을 입력하는 것이 불가능해서 에러가 발생한다.
      null을 저장할 수 있는 Integer 형으로 변환하거나, 밑에서 알아볼 defaultValue를 사용해야 한다!


5. requestParamDefault

@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(
    @RequestParam(required = true, defaultValue = "guest") String username,
    @RequestParam(required = false, defaultValue = "-1") Integer age) {

    log.info("username={}, age={}", username, age);
    return "ok";
}
  • 파라미터에 값이 없는 경우,
    defaultValue를 사용하여 기본 값을 적용할 수 있다.
  • defaultValue를 사용하면, (이미 기본 값이 있기 때문에) required는 큰 의미가 없으므로 빼도 된다.
  • 빈 문자("")인 경우에도 설정한 기본 값이 적용된다.


6. requestParamMap

 @ResponseBody
 @RequestMapping("/request-param-map")
 public String requestParamMap(@RequestParam Map<String, Object> paramMap) {

    log.info("username={}, age={}", paramMap.get("username"), paramMap.get("age"));
    return "ok";
}

이전까지는 요청 파라미터를 하나씩 받았지만, Map을 이용해서 한 번에 받을 수도 있다!


HTTP 요청 파라미터 - @ModelAttribute

@RequestParam을 이용해서 요청 파라미터를 하나하나 받아주었다.

하지만 요청 파라미터가 하나의 객체가 되어야 하는 경우, 각각 파라미터 요청을 조회해서 객체를 생성하고 값을 넣어주는 작업을 해줘야 한다.

생각만 해도 너무 번거로운데, 스프링에서는 이 과정을 @ModelAttribute를 통해 완전히 자동화해준다!


HelloData

package hello.springmvc.basic;

import lombok.Data;

@Data
public class HelloData {
    private String username;
    private int age;
}
  • 요청 파라미터를 바인딩할 객체를 만들었다.
  • @Data: @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor를 자동으로 적용해준다!

modelAttributeV1

@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData) {

    log.info("username={}, age={}", helloData.getUsername(), helloData.getAge());
    return "ok";
}

@ModelAttibute가 있을 때의 동작

  1. HelloData 객체를 생성한다.
  2. 요청 파라미터의 이름으로 HelloData 객체의 프로퍼티를 찾는다.
    해당 프로퍼티의 setter를 호출해서 파라미터의 값을 바인딩한다.

✔ 프로퍼티

  • 객체에 getUsername(), setUsername() 메서드가 있으면, 이 객체는 username이라는 프로퍼티를 가지고 있다.
  • username 프로퍼티의 값을 변경하면 setUsername()이 호출되고, 조회하면 getUsername()이 호출된다.

modelAttributeV2

 @ResponseBody
 @RequestMapping("/model-attribute-v2")
 public String modelAttributeV2(HelloData helloData) {

    log.info("username={}, age={}", helloData.getUsername(), helloData.getAge());
    return "ok";
}

@ModelAttribute는 생략할 수 있다.

@ModelAttribute를 생략 가능하다고,,? 앞에서 RequestParam도 생략 가능하다고 했는데? 😨
그러면 스프링은 어떤 애노테이션이 생략되었는지 어떻게 알까??

📌 스프링은 생략 시 다음과 같은 규칙을 적용한다.

  • String, int, Integer 값은 단순 타입 = @RequestParam
  • 나머지 = @ModelAttribute (argument resolver로 지정해둔 타입 외)

HTTP 요청 메시지 - 단순 텍스트

요청 파라미터와 다르게, HTTP 메시지 바디를 통해 데이터가 직접 넘어오는 경우는 @RequestParam, @ModelAttribute를 사용할 수 없다!

1. RequestBodyStringController

@PostMapping("/request-body-string-v1")
public void requestBodyString(HttpServletRequest request, HttpServletResponse response) throws IOException {

    ServletInputStream inputStream = request.getInputStream();
    String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

    log.info("messageBody={}", messageBody);
    response.getWriter().write("ok");
}

HTTP 메시지 바디의 데이터를 InputStream을 이용해서 직접 읽을 수 있다.


2. Input, Output 스트림, Reader - requestBodyStringV2

@PostMapping("/request-body-string-v2")
public void requestBodyStringV2(InputStream inputStream, Writer responseWriter)
throws IOException {
    String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
    log.info("messageBody={}", messageBody);
    responseWriter.write("ok");
}
  • InputStream(Reader): HTTP 요청 메시지 바디의 내용을 직접 조회
  • OutputStream(Writer): HTTP 응답 메시지 바디에 직접 결과 조회


3. HttpEntity - requestBodyStringV3

HttpEntity를 사용하면 더 편리하게 조회할 수 있다.

@PostMapping("/request-body-string-v3")
public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) {
    String messageBody = httpEntity.getBody();
    log.info("messageBody={}", messageBody);
    return new HttpEntity<>("ok");
}
  • HttpEntity: HTTP header, body 정보를 편리하게 조회
    • 메시지 바디 정보 직접 조회 가능(getBody())
    • 요청 파라미터를 조회하는 기능과는 관계 없음. (@RequestParam, @ModelAttribute)
  • HttpEntity응답에도 사용 가능
    • 메시지 바디 정보 직접 반환
    • 헤더 정보 포함 가능
    • View 조회 X


4. @RequestBody - requestBodyStringV4

@RequestBody를 사용하면 HTTP 메시지 바디 정보를 편리하게 조회할 수 있다.

@ResponseBody
@PostMapping("/request-body-string-v4")
public String requestBodyStringV4(@RequestBody String messageBody) {
    log.info("messageBody={}", messageBody);
    return "ok";
}
  • 헤더 정보가 필요하다면 HttpEntity를 사용하거나 @RequestHeader 사용!
  • 이렇게 메시지 바디를 직접 조회하는 기능은 요청 파라미터를 조회하는 @RequestParam,
    @ModelAttribute와는 전혀 관계가 없다.

📌 요청 파라미터 vs HTTP 메시지 바디

  • 요청 파라미터를 조회하는 기능: @RequestParam, @ModelAttiribute
  • HTTP 메시지 바디를 직접 조회하는 기능: @RequestBody

HTTP 요청 메시지 - JSON

이번에는 단순 텍스트가 아닌 JSON 데이터 형식을 조회해보자!

1. requestBodyJsonController

@PostMapping("/request-body-json-v1")
public void requestBodyJsonV1(HttpServletRequest request, HttpServletResponse response) throws IOException {

    ServletInputStream inputStream = request.getInputStream();
    String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

    log.info("messageBody={}", messageBody);
    HelloData data = objectMapper.readValue(messageBody, HelloData.class);
    log.info("username={}, age={}", data.getUsername(), data.getAge());

    response.getWriter().write("ok");
}
  • HttpServletRequest를 사용해서 직접 HTTP 메시지 바디에서 데이터를 읽어와서, 문자로 변환한다.
  • 문자로 된 JSON 데이터를 objectMapper를 사용해서 자바 객체로 변환한다.


2. requestBodyJsonV2 - @RequestBody 문자 변환

@ResponseBody
@PostMapping("/request-body-json-v2")
public String requestBodyJsonV2(@RequestBody String messageBody) throws IOException {

    HelloData data = objectMapper.readValue(messageBody, HelloData.class);
    log.info("username={}, age={}", data.getUsername(), data.getAge());

    return "ok";
}
  • @RequestBody를 사용해서 HTTP 메시지에서 데이터를 꺼내고 messageBody에 저장한다.
  • 문자로 된 JSON 데이터인 messageBodyobjectMapper를 통해서 자바 객체로 변환한다.

위의 두 가지 방법 모두 문자로 변환하고 다시 json으로 변환하는 과정이 있다! @ModelAtrribute 처럼 한 번에 객체로 변환할 수는 없을까?🤔


requestBodyJsonV3 - @RequestBody 객체 변환

@RequestBody를 사용하면 객체를 직접 지정해서 매핑해줄 수 있다!

 @ResponseBody
 @PostMapping("/request-body-json-v3")
 public String requestBodyJsonV3(@RequestBody HelloData data) throws IOException {

    log.info("username={}, age={}", data.getUsername(), data.getAge());
    return "ok";
}
  • HttpEntity, @RequestBody를 사용하면, HTTP 메시지 컨버터가 HTTP 메시지 바디의 내용을 우리가 원하는 문자나 객체 등으로 변환해준다.
  • @RequestBody는 생략할 수 없다!
    • HelloData@RequestBody를 생략하면 @ModelAttribute가 적용되어 버린다.
    • 그래서 생략하면 HTTP 메시지 바디가 아니라 요청 파라미터를 처리하게 된다.


requestBodyJsonV4 - HttpEntity, requestBodyJsonV5

응답도 @ResponseBody를 사용하면 해당 객체를 HTTP 메시지 바디에 직접 넣어줄 수 있다.

@ResponseBody
@PostMapping("/request-body-json-v4")
public String requestBodyJsonV4(HttpEntity<HelloData> httpEntity) {

    HelloData data = httpEntity.getBody();
    log.info("username={}, age={}", data.getUsername(), data.getAge());
    return "ok";
}
@ResponseBody
@PostMapping("/request-body-json-v5")
public HelloData requestBodyJsonV5(@RequestBody HelloData data) {

    log.info("username={}, age={}", data.getUsername(), data.getAge());
    return data;
}
  • @RequestBody 요청: JSON 요청 → HTTP 메시지 컨버터 → 객체
  • @ResponseBody 응답: 객체 → HTTP 메시지 컨버터 → JSON 응답

profile
🚧 https://coji.tistory.com/ 🏠

0개의 댓글