[Spring] HTTP Request & Response Message

Goghยท2023๋…„ 1์›” 2์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
12/23

๐ŸŽฏ ๋ชฉํ‘œ : Spring MVC์—์„œ์˜ HTTP ์š”์ฒญ / ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํ•™์Šต๋‚ด์šฉ ์ •๋ฆฌ

๐Ÿ“’ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์กฐํšŒ


๐Ÿ“Œ HttpServletRequest & @RequestParam & @ModelAttribute

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก ๋ฐ›์•„ ์กฐํšŒํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•.
# GET ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก
http://localhost:8080/request-param?username=lee&age=20

# POST HTML Form ํ˜•์‹์œผ๋กœ ์ „์†ก
POST /request-param ...
content-type: application/x-www-form-urlencoded
username=lee&age=20
  • ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก ๋ฐ›๋Š”๋‹ค ๊ฐ€์ •ํ•˜๊ณ  ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ–ˆ๋‹ค.
// ๋™์ผํ•œ ๋งตํ•‘์œผ๋กœ log๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•˜๋‚˜์”ฉ ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ•ด์•ผํ•œ๋‹ค.
@Slf4j
@Controller
public class RequestParamController {
    @RquestMapping("/request-param")
    public void requestParamServlet(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");
    }

    @ResponseBody /* View ์กฐํšŒ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  HTTP message Body์— String ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ „์†กํ•œ๋‹ค */
    @RequestMapping("/request-param")
    public String requestParamAnnotation(
            @RequestParam String username,
            @RequestParam int age
    ) {
        log.info("username={}, age={}", username, age);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/request-param")
    public String requestParamBasic(String username, int age) {
        log.info("username={}, age={}", username, age);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/request-param")
    public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
        log.info("username={}, age={}",
          paramMap.get("username"),
          paramMap.get("age"));
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/request-param")
    private String modelAttributeV1(@ModelAttribute HelloData helloData) {
        /* HelloData ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•ด์ค€๋‹ค.*/
        log.info("HelloData = {}",helloData);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/request-param")
    private String modelAttributeV2(HelloData helloData) {
        /* HelloData ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•ด์ค€๋‹ค.*/

        log.info("HelloData = {}",helloData);
        return "ok";
    }
}
  • request.getParameter๋Š” ๋‹จ์ˆœํžˆ HttpServletRequest๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
  • @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์€ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒ ํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
    • ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ์ž๋™์œผ๋กœ ํ• ๋‹นํ•ด์ค€๋‹ค.
    • @RequestParam("username") String memberName๊ณผ ๊ฐ™์ด key ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๋„ค์ด๋ฐ์€ ์ง์ ‘ ์ง€์ •ํ• ์ˆ˜ ์žˆ๋‹ค.
    • @RequestParam(required = true, defaultValue = "guest") String username
    • @RequestParam(required = true, defaultValue = "-1") int age
    • ์œ„ ์˜ˆ์‹œ์™€ ๊ฐ™์ด required ๊ฐ’์œผ๋กœ ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์ •ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, null๊ณผ ๋น„์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ๊ฐ’์„ ์ง€์ • ํ• ์ˆ˜ ์žˆ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ String, int ๋“ฑ ๊ธฐ๋ณธํ˜• ์ด๋ผ๋ฉด, @RequestParam์„ ์ƒ๋žตํ•˜๊ณ  key ๊ฐ’๊ณผ ๊ฐ™์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  • @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์€ Map ํ˜•์‹์œผ๋กœ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„์ˆ˜ ์žˆ๋‹ค.
  • @ModelAttribute ์–ด๋…ธํ…Œ์ด์…˜์€ ๊ฐ์ฒด์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉ ํ•ด์ค€๋‹ค.
    • HelloData ํด๋ž˜์Šค๋Š” username๊ณผ age๋ฅผ ํ•„๋“œ ๋งด๋ฒ„๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  setter, getter์™€ ํ•„์š”ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.
    • @ModelAttribute๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋งค์นญ๋˜๋Š” ํ•„๋“œ ๋งด๋ฒ„๊ฐ€ ์žˆ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•ด์ค€๋‹ค.
      1. HelloData ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
      2. ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์œผ๋กœ HelloData ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์˜ setter๋ฅผ ํ˜ธ์ถœํ•ด์„œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค.
      3. ex) username -> steUsername()
  • @ModelAttribute๊ณผ @RequestParam ๋‘˜๋‹ค ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. ์–ด๋–ค ๊ธฐ๋Šฅ์ด ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋ ๊นŒ?
    • String, int, Integer์™€ ๊ฐ™์€ ๋‹จ์ˆœ ๊ธฐ๋ณธํ˜• ํƒ€์ž…์ผ๋•Œ๋Š” @RequestParam์ด ์šฐ์„ ์œผ๋กœ ์ ์šฉ๋œ๋‹ค.
    • ๋‚˜๋จธ์ง€, ๊ฐ์ฒด๋“ฑ @ModelAttribute๊ฐ€ ์ ์šฉ๋œ๋‹ค.

๐Ÿ“’ HTTP ์š”์ฒญ ๋ฉ”์„ธ์ง€์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์กฐํšŒ


๐Ÿ“Œ HTTP Message Body

  • HTTP API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐฉ์‹์€ JSON์œผ๋กœ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ๋งŒ ์ •๋ฆฌ ํ•˜์˜€๋‹ค.
  • POST, PUT, PATCH์— ์ ์šฉ๋œ๋‹ค.
  • ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋‹ค๋ฅด๊ฒŒ HTTP ๋ฉ”์„ธ์ง€ ๋ฐ”๋””๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋„˜์–ด์˜ค๋Š” ๊ฒฝ์šฐ @RequestParam๊ณผ @ModelAttribute๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.(HTML Form ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ์ œ์™ธ)
  • raw, JSON, content-type: application/json
    {"username":"Lee", "age":20} ์™€ ๊ฐ™์ด JSON ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์š”์ฒญ ๋ฉ”์„ธ์ง€๋กœ ๋“ค์–ด ์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค
@Controller
@Slf4j
public class RequestBodyJsonController {

    private ObjectMapper objectMapper = new ObjectMapper();

    @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");
    }

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

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

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

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

    @ResponseBody
    @PostMapping("/request-body-json-v4")
    public String requestBodyJsonV4(HttpEntity<HelloData> data) {
        HelloData helloData = data.getBody();
        log.info("username={}, age={}", helloData.getUsername(), helloData.getAge());
        return "ok";
    }
}
  • InputStream, OutputStream์„ ํ™œ์šฉํ•˜์—ฌ Stringํƒ€์ž…์˜ ๋‹จ์ˆœ ๋ฉ”์„ธ์ง€ ๋ฐ”๋””์˜ ๋‚ด์šฉ์„ ์กฐํšŒํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ ํ• ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ HttpServletRequest request, HttpServletResponse response๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ObjectMapper๋กœ ๋งตํ•‘ํ•˜์—ฌ JSON ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด์— ์ ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • @RequestBody๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HTTP ๋ฉ”์„ธ์ง€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด๊ณ  ObjectMapper๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋งตํ•‘ ํ•œ๋‹ค.
  • @RequestBody๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง์ ‘ ๊ฐ์ฒด์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๋ฉด, ๋ณ„๋„์˜ ๋งตํ•‘์ด ํ•„์š” ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„์ˆ˜ ์žˆ๋‹ค.(HelloData)
  • HttpEntity๋ฅผ ํ™œ์šฉํ•˜์—ฌ HTTP Header,Body๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
    • HttpEntity๋ฅผ ์ƒ์† ๋ฐ›์€ RequestEntity์™€ ResponseEntity๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • HttpEntity์™€ @RequestBody๋Š” HTTP ๋ฉ”์„ธ์ง€ ์ปจ๋ฒ„ํ„ฐ๊ฐ€ HTTP ๋ฉ”์„ธ์ง€ ๋ฐ”๋””์˜ ๋‚ด์šฉ์„ ์›ํ•˜๋Š” ๋ฌธ์ž๋‚˜ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ ํ•ด ์ค€๋‹ค. ๋ฌธ์ž ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ JSON ํƒ€์ž…์˜ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜๋„ ์ง€์›ํ•œ๋‹ค.

๐Ÿ“’ HTTP API ๋ฉ”์„ธ์ง€ ๋ฐ”๋””์— ์ง์ ‘ ์‘๋‹ต


๐Ÿ“Œ HTTP Response Body

  • HTTP API๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, HTTP ๋ฉ”์„ธ์ง€ ๋ฐ”๋””์— JSON ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ตํ•œ๋‹ค. ๋‹จ์ˆœ String ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋„ ์‘๋‹ต ๊ฐ€๋Šฅํ•˜๋‹ค.
@Slf4j
@Controller
public class ResponseBodyController {

    @GetMapping("/response-body-string-v1")
    public void responseBodyV1(HttpServletResponse response) throws IOException {
        response.getWriter().write("ok");

    }

    @GetMapping("/response-body-string-v2")
    public ResponseEntity<String> responseBodyV2() {
        return new ResponseEntity<>("ok", HttpStatus.OK);
    }

    @ResponseBody
    @GetMapping("/response-body-string-v3")
    public String responseBodyV3() {
        return "ok";
    }

    @GetMapping("/response-body-json-v1")
    public ResponseEntity<HelloData> responseBodyJsonV1() {
        HelloData helloData = new HelloData();
        helloData.setUsername("userA");
        helloData.setAge(20);
        return new ResponseEntity<>(helloData, HttpStatus.OK);
    }

    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    @GetMapping("/response-body-json-v2")
    public HelloData responseBodyJsonV2() {
        HelloData helloData = new HelloData();
        helloData.setUsername("userA");
        helloData.setAge(20);
        return helloData;
    }
}
  • HttpServletResponse๊ฐ์ฒด๋ฅผ ํ†ตํ•ด HTTP ๋ฉ”์„ธ์ง€ ๋ฐ”๋””์— ์ง์ ‘ ์‘๋‹ต ๋ฉ”์„ธ์ง€๋ฅผ ์ „๋‹ฌ ํ• ์ˆ˜ ์žˆ๋‹ค.
  • ResponseEntity๋Š” HttpEntity๋ฅผ ์ƒ์† ๋ฐ›์•„ HTTP ๋ฉ”์„ธ์ง€์˜ ํ—ค๋”์™€ ๋ฐ”๋”” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, HTTP ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ ํ• ์ˆ˜ ์žˆ๋‹ค.
  • @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด HTTP ๋ฉ”์„ธ์ง€ ์ปด๋ฒ„ํ„ฐ๋ฅผ ํ†ตํ•ด HTTP ๋ฉ”์„ธ์ง€๋ฅผ ์ง์ ‘ ์ž…๋ ฅ ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, ResponseEntity์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ ํ•œ๋‹ค.
    • ResponseBody์—๋Š” ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉ ํ• ์ˆ˜ ์—†๋Š”๋ฐ @ResponseStatus(HttpStatus.OK)์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜์—ฌ ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ์„ค์ • ํ• ์ˆ˜ ์žˆ๋‹ค.
  • ResponseEntity๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด HTTP ๋ฉ”์„ธ์ง€ ์ปจ๋ฒ„ํ„ฐ๋ฅผ ํ†ตํ•˜์—ฌ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜๋˜์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

HTTP ์š”์ฒญ ๋ฐ”๋””์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ๊ณผ์ • ์ •๋ฆฌ

  • HTTP ๋ฉ”์„ธ์ง€ ์ปจ๋ฒ„ํ„ฐ์˜ ๋™์ž‘๊ณผ ์ฒ˜๋ฆฌ ๊ณผ์ •

HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

  • HTTP ์š”์ฒญ์ด ์˜ค๊ณ , ์ปจํŠธ๋กค๋Ÿฌ์—์„œ @RequestBody , HttpEntity ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฉ”์‹œ์ง€ ์ปจ๋ฒ„ํ„ฐ๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด canRead ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    • ๋Œ€์ƒ ํด๋ž˜์Šค ํƒ€์ž…์„ ์ง€์›ํ•˜๋Š”์ง€ ํ™•์ธ.
      • ex) @RequestBody ์˜ ๋Œ€์ƒ ํด๋ž˜์Šค ( byte[] , String , HelloData )
    • HTTP ์š”์ฒญ์˜ Content-Type ๋ฏธ๋””์–ด ํƒ€์ž…์„ ์ง€์› ํ•˜๋Š”์ง€ ํ™•์ธ.
      • ex) text/plain , application/json , */*
  • canRead ๋ฉ”์†Œ๋“œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด read() ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ณ , ๋ฐ˜ํ™˜ํ•œ๋‹ค.

HTTP ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

  • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ @ResponseBody , HttpEntity ๋กœ ๊ฐ’์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
  • ๋ฉ”์‹œ์ง€ ์ปจ๋ฒ„ํ„ฐ๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด canWrite๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    • ๋Œ€์ƒ ํด๋ž˜์Šค ํƒ€์ž…์„ ์ง€์›ํ•˜๋Š”์ง€ ํ™•์ธ.
      • ex) return์˜ ๋Œ€์ƒ ํด๋ž˜์Šค ( byte[] , String , HelloData )
    • HTTP ์š”์ฒญ์˜ Accept ๋ฏธ๋””์–ด ํƒ€์ž…์„ ์ง€์›ํ•˜๋Š”์ง€ ํ™•์ธ.(@RequestMapping ์˜ produces ๊ฐ’)
      • ex) text/plain , application/json , */*
  • canWrite ๋ฉ”์†Œ๋“œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด write() ๋ฅผ ํ˜ธ์ถœํ•ด์„œ HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
profile
์ปดํ“จํ„ฐ๊ฐ€ ํ• ์ผ์€ ์ปดํ“จํ„ฐ๊ฐ€

0๊ฐœ์˜ ๋Œ“๊ธ€