Post

Spring HTTP 요청 메시지 조회

1. JSON으로 보낸 HTTP 요청 메시지

1) HttpServletRequest, HttpServletResponse 이용

HttpServletRequest를 사용하여 HTTP 메시지 바디 데이터를 읽어 문자로 변환한다.

Stream은 바이트코드이기 때문에 인코딩 설정을 해야 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
public class ApiController {
    // HttpServletRequest, HttpServletResponse
    @PostMapping("/servlet")
    public void servlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String bodyMessage = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        log.info("bodyMessage={}", bodyMessage);

        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("Content-Type", "application/json;charset=utf-8");
        response.setContentType("application/json");
        response.getWriter().write(bodyMessage);
    }
}

2) @RequestBody String 타입

@RequestBody는 요청 메시지 바디를 조회할 수 있는 어노테이션이다.

@ReqiestHeader는 헤더 정보를 조회할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ApiController {
    // @RequestBody String
    @PostMapping("/requestBodyString")
    public String requestBody(@RequestBody String bodyMessage) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper(); // jackson
        DataDto dataDto = objectMapper.readValue(bodyMessage, DataDto.class);
        log.info("id={}, name={}", dataDto.getId(), dataDto.getName());

        return bodyMessage;
    }
}

3) @RequestBody 객체 타입

HTTP 메시지 컨버터는 JSON을 객체로 변환해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ApiController {
    // @RequestBody 객체
    @PostMapping("/requestBodyDto")
    public DataDto requestBodyJsonV3(@RequestBody DataDto dataDto) {
        log.info("id={}, name={}", dataDto.getId(), dataDto.getName());

        return dataDto;
    }
}

[출처 및 참고]

This post is licensed under CC BY 4.0 by the author.