# Spring MVC / HttpServletRequest
Study Repository

Spring MVC / HttpServletRequest

by rlaehddnd0422

HttpServletRequest 

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒ์‹ฑํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ HttpServletRequest ๊ฐ์ฒด์— ๋‹ด์•„์„œ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

HttpServletRequest๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€ 

POST /save HTTP/1.1    // start-line
Host : localhost:8080   // header
Content-Type : application/x-www-unlencoded

username=dongwoong&age=25  // body

์ถ”๊ฐ€์ ์œผ๋กœ ์ž„์‹œ ์ €์žฅ์†Œ ๊ธฐ๋Šฅ, ์„ธ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

1) ์ž„์‹œ ์ €์žฅ์†Œ ๊ธฐ๋Šฅ์€ ํ•ด๋‹น HTTP ์š”์ฒญ์ด ์‹œ์ž‘๋ถ€ํ„ฐ ๋๋‚  ๋•Œ ๊นŒ์ง€ ์œ ์ง€๋˜๋Š” ์ž„์‹œ์ €์žฅ์†Œ ๊ธฐ๋Šฅ์œผ๋กœ, 

setAttribute(name,value)๋ฅผ ํ†ตํ•ด ์ €์žฅ

getAttribute(name)์„ ํ†ตํ•œ value ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

2) ์„ธ์…˜ ๊ด€๋ฆฌ๊ธฐ๋Šฅ์€ getSession(create:true)๋ฅผ ํ†ตํ•ด ์„ธ์…˜์„ ๊ด€๋ฆฌํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

HttpServletRequest, HttpServletResponse ๋ฅผ ๋ช…ํ™•ํžˆ ์ดํ•ดํ•˜๋ ค๋ฉด HTTP ์ŠคํŽ™์ด ์ œ๊ณตํ•˜๋Š” request, response ๋ฉ”์‹œ์ง€ ์ž์ฒด๋ฅผ ์ดํ•ดํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ „ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ .

 

https://rlaehddnd0422.tistory.com/45

 

HTTP ํ‘œํ˜„ ํ—ค๋”

message body(ํŽ˜์ด๋กœ๋“œ)๋ฅผ ํ†ตํ•ด ํ‘œํ˜„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ‘œํ˜„ ๋ฐ์ดํ„ฐ๋Š” ์š”์ฒญ์ด๋‚˜ ์‘๋‹ต์—์„œ ์ „๋‹ฌํ•  ์‹ค์ œ ๋ฐ์ดํ„ฐ ํ‘œํ˜„ ํ—ค๋”๋Š” ํ‘œํ˜„ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณต ํ•ด์„ ์ •๋ณด -> ๋ฐ์ดํ„ฐ ์œ ํ˜•,

rlaehddnd0422.tistory.com

https://rlaehddnd0422.tistory.com/42

 

HTTP ๋ฉ”์†Œ๋“œ - GET, POST, PUT, PATCH, DELETE in Start line

ํšŒ์› ์ •๋ณด ๊ด€๋ฆฌ API๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•  ๋•Œ, 5๊ฐ€์ง€ ์š”๊ตฌ์‚ฌํ•ญ์ด ์ฃผ์–ด์ง„๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. 1. ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ 2. ํšŒ์› ์กฐํšŒ 3. ํšŒ์› ๋“ฑ๋ก 4. ํšŒ์› ์ˆ˜์ • 5. ํšŒ์› ์‚ญ์ œ API URI๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ๋Š” ์šฐ์„  ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„ํ•˜๋Š”

rlaehddnd0422.tistory.com


HttpServletRequest ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

package hello.servlet.basic.request;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
    {
        printStartLine(request);
        printHeaders(request);
        printHeaderUtils(request);
        printEtc(request);
    }

    private void printStartLine(HttpServletRequest request) {
        System.out.println("--- REQUEST-LINE - start ---");
        System.out.println("request.getMethod() = " + request.getMethod()); //GET
        System.out.println("request.getProtocol() = " + request.getProtocol()); // HTTP 1.1
        System.out.println("request.getScheme() = " + request.getScheme()); //http
        // http://localhost:8080/request-header
        System.out.println("request.getRequestURL() = " + request.getRequestURL());
        // /request-header
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        //username=hi
        System.out.println("request.getQueryString() = " +
                request.getQueryString());
        System.out.println("request.isSecure() = " + request.isSecure()); //http ์‚ฌ์šฉ ์œ ๋ฌด
        System.out.println("--- REQUEST-LINE - end ---");
        System.out.println();
    }

    private void printHeaders(HttpServletRequest request) {
        System.out.println("--- Headers - start ---");
        /*
         Enumeration<String> headerNames = request.getHeaderNames();
         while (headerNames.hasMoreElements()) {
         String headerName = headerNames.nextElement();
         System.out.println(headerName + ": " + request.getHeader(headerName));
        }
        */

        request.getHeaderNames().asIterator()
                .forEachRemaining(headerName -> System.out.println(headerName + ":" + request.getHeader(headerName)));

        System.out.println("--- Headers - end ---");
        System.out.println();
    }

    private void printHeaderUtils(HttpServletRequest request)
    {
        System.out.println("--- Header ํŽธ์˜ ์กฐํšŒ start ---");
        System.out.println("[Host ํŽธ์˜ ์กฐํšŒ]");
        System.out.println("request.getServerName() = " +  request.getServerName()); //Host ํ—ค๋”
        System.out.println("request.getServerPort() = " + request.getServerPort()); //Host ํ—ค๋”
        System.out.println();

        System.out.println("[Accept-Language ํŽธ์˜ ์กฐํšŒ]"); request.getLocales().asIterator()
                        .forEachRemaining(locale -> System.out.println("locale = " + locale));
        System.out.println("request.getLocale() = " + request.getLocale());
        System.out.println();

        System.out.println("[cookie ํŽธ์˜ ์กฐํšŒ]");
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies())
            {
                System.out.println(cookie.getName() + ": " + cookie.getValue());
            }
        }
        System.out.println();

        System.out.println("[Content ํŽธ์˜ ์กฐํšŒ]");
        System.out.println("request.getContentType() = " + request.getContentType());
        System.out.println("request.getContentLength() = " + request.getContentLength());
        System.out.println("request.getCharacterEncoding() = " + request.getCharacterEncoding());


        System.out.println("--- Header ํŽธ์˜ ์กฐํšŒ end ---");
        System.out.println();
    }

    private void printEtc(HttpServletRequest request) { System.out.println("--- ๊ธฐํƒ€ ์กฐํšŒ start ---");
        System.out.println("[Remote ์ •๋ณด]");
        System.out.println("request.getRemoteHost() = " +
                request.getRemoteHost()); //
        System.out.println("request.getRemoteAddr() = " +
                request.getRemoteAddr()); //
        System.out.println("request.getRemotePort() = " +
                request.getRemotePort()); //
        System.out.println();
        System.out.println("[Local ์ •๋ณด]");
        System.out.println("request.getLocalName() = " +
                request.getLocalName()); //
        System.out.println("request.getLocalAddr() = " +
                request.getLocalAddr()); //
        System.out.println("request.getLocalPort() = " +
                request.getLocalPort()); //
        System.out.println("--- ๊ธฐํƒ€ ์กฐํšŒ end ---");
        System.out.println();
    }


}

Start-Line ์ •๋ณด

  • getMethod() : ๋ฉ”์†Œ๋“œ ์ •๋ณด๋ฅผ ๋ฆฌํ„ด ( GET, POST .. )
  • getProtocol() : ํ”„๋กœํ† ์ฝœ ์ •๋ณด๋ฅผ ๋ฆฌํ„ด ( HTTP/1.1 ) 
  • getScheme() : ํ”„๋กœํ† ์ฝœ ์ •๋ณด๋ฅผ ๋ฆฌํ„ด ( http, https .. )
  • getRequestURL() : ์š”์ฒญ URL ๋ฆฌํ„ด ( http://localhost:8080/request-header )
  • getRequestURI() : URI๋งŒ ๋ฆฌํ„ด ( request-header )
  • getQueryString() : query ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฆฌํ„ด ( ์—†์œผ๋ฉด null ๋ฆฌํ„ด )
  • isSecure() : https ์‚ฌ์šฉ ์œ ๋ฌด ๋ฆฌํ„ด 

Header ์ •๋ณด

  • getServerName() : ์„œ๋ฒ„ ์ด๋ฆ„ ์กฐํšŒ 
  • getServerPort() : ์„œ๋ฒ„ ํฌํŠธ๋ฒˆํ˜ธ ์กฐํšŒ
  • getHeaderNames() : ํ—ค๋” ์กฐํšŒ
  • asIterator().forEachRemaining()์„ ํ†ตํ•ด ๋ชจ๋“  ํ—ค๋”์ด๋ฆ„๊ณผ ํ—ค๋” ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • getLocale() : ์šฐ์„ ์ˆœ์œ„ ๊ฐ€์žฅ ๋†’์€ Accept-Language ์กฐํšŒ
  • asIterator().forEachRemaining()์„ ํ†ตํ•ด ๋ชจ๋“  Language ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 
  • getCookies() : ์ฟ ํ‚ค ์กฐํšŒ 
  • getContentType() : ์ฝ˜ํ…ํŠธํƒ€์ž… ์กฐํšŒ
  • getContentLength() : ์ฝ˜ํ…ํŠธ ๊ธธ์ด ์กฐํšŒ
  • getCharacterEncoding : ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ์กฐํšŒ
request.getLocales().asIterator()
                        .forEachRemaining(locale -> System.out.println("locale = " + locale));

 

๊ทธ ์™ธ ๊ธฐํƒ€ ์ •๋ณด ( ํฌํŠธ๋ฒˆํ˜ธ, ํ˜ธ์ŠคํŠธ์ฃผ์†Œ ๋“ฑ๋“ฑ ) ์กฐํšŒ ๊ฐ€๋Šฅ.

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ

--- REQUEST-LINE - start ---
request.getMethod() = GET
request.getProtocol() = HTTP/1.1
request.getScheme() = http
request.getRequestURL() = http://localhost:8080/request-header
request.getRequestURI() = /request-header
request.getQueryString() = null
request.isSecure() = false
--- REQUEST-LINE - end ---

--- Headers - start ---
host:localhost:8080
connection:keep-alive
cache-control:max-age=0
sec-ch-ua:"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"
sec-ch-ua-mobile:?0
sec-ch-ua-platform:"macOS"
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
sec-fetch-site:none
sec-fetch-mode:navigate
sec-fetch-user:?1
sec-fetch-dest:document
accept-encoding:gzip, deflate, br
accept-language:ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
--- Headers - end ---

--- Header ํŽธ์˜ ์กฐํšŒ start ---
[Host ํŽธ์˜ ์กฐํšŒ]
request.getServerName() = localhost
request.getServerPort() = 8080

[Accept-Language ํŽธ์˜ ์กฐํšŒ]
locale = ko_KR
locale = ko
locale = en_US
locale = en
request.getLocale() = ko_KR

[cookie ํŽธ์˜ ์กฐํšŒ]

[Content ํŽธ์˜ ์กฐํšŒ]
request.getContentType() = null
request.getContentLength() = -1
request.getCharacterEncoding() = UTF-8
--- Header ํŽธ์˜ ์กฐํšŒ end ---

--- ๊ธฐํƒ€ ์กฐํšŒ start ---
[Remote ์ •๋ณด]
request.getRemoteHost() = 0:0:0:0:0:0:0:1
request.getRemoteAddr() = 0:0:0:0:0:0:0:1
request.getRemotePort() = 60763

[Local ์ •๋ณด]
request.getLocalName() = localhost
request.getLocalAddr() = 0:0:0:0:0:0:0:1
request.getLocalPort() = 8080
--- ๊ธฐํƒ€ ์กฐํšŒ end ---

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

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์ฃผ๋กœ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

1. GET - ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฌ์šฉ 

  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์—†์ด, URL์˜ ์ฟผ๋ฆฌํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ex) ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง•์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ 
  • ์‚ฌ์šฉ ์˜ˆ์‹œ : url?name=kim&age=25
package hello.servlet.basic.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/*
* 1. ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก๊ธฐ๋Šฅ
* http://localhost:8080/request-param?username=hello&age=20
*
 */

@WebServlet(name="requestParamServlet", urlPatterns = "/request-param")
public class RequestParamServlet extends HttpServlet
{
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        System.out.println("[์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]-start");
        request.getParameterNames().asIterator()
            .forEachRemaining(paramName -> System.out.println(paramName + "=" + request.getParameter(paramName)));
        System.out.println("[์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]-end");
        System.out.println();

        System.out.println("[๋‹จ์ผ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]-start");
        String name = request.getParameter("name");
        System.out.println("name = " + name);
        String age = request.getParameter("age");
        System.out.println("age = " + age);
        System.out.println();


        System.out.println("[์ด๋ฆ„์ด ๊ฐ™์€ ๋ณต์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]");
        String[] names = request.getParameterValues("name");
        for (String s : names) {
            System.out.println(s);
        }

        response.getWriter().write("OK.");
    }
}

  1. localhost:8080/request-param์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ URI์ธ localhost:8080/request-param?name=kim&age=20๋ฅผ ์„œ๋ฒ„์— ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
  2.  HttpServletRequest ๊ฐ์ฒด์ธ request์— ์š”์ฒญ ์ •๋ณด(ํŒŒ๋ผ๋ฏธํ„ฐ)๋ฅผ ๋‹ด์•„์„œ ์„œ๋ฒ„์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
  3.  ์„œ๋ฒ„๋Š” HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ HttpServletResponse ๊ฐ์ฒด์— ๋‹ด์•„ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

 

request.getParameter("ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„") ์„ ํ†ตํ•ด ์š”์ฒญํ•  ๋•Œ ์„œ๋ฒ„์— ๋ณด๋‚ธ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์— ๋Œ€ํ•œ ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘๋ณต๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ request.getParameterValues()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ค‘๋ณต ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ๊ฐ’๋“ค์„ ๋ชจ๋‘ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Postman์„ ์ด์šฉํ•œ GET

2. POST - HTML FORM ์‚ฌ์šฉ

  • content-type : application/x-www-form-urlencoded ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ex) ํšŒ์› ๊ฐ€์ž…, ์ƒํ’ˆ ์ฃผ๋ฌธ

html-form.html

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

html-form.html

์—ฌ๊ธฐ์„œ ์ž‘์„ฑํ•œ ํผ์€ /request-param์— POST ๋ฐฉ์‹์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ๋’ค์— ์ „์†ก์„ ๋ˆ„๋ฅด๋ฉด request message body์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

name=kdo6301&age=11 ์ด ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ๋‹ด๊ฒจ /request-param์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

 

Postman์„ ์ด์šฉํ•œ POST

GET URL ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” HTTP ๋ฐ”๋””๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Content-type์ด ์—†์ง€๋งŒ, POST HTML Form ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋””์— ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ํ˜•์‹์ธ์ง€ Content-type์„ ๊ผญ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํผ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ํ˜•์‹์€ Content-type : application/x-www-form-urlencoded์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

3. HTTP message body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ด์•„์„œ ์š”์ฒญ 

  • ์ฃผ๋กœ HTTP API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (json, xml, text)
  • ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ์ฃผ๋กœ json ์‚ฌ์šฉ ( POST, PUT, PATCH )

3-1. TEXT

 

HTTP message body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ด์•„์„œ ์š”์ฒญ

package hello.servlet.basic.request;

import org.springframework.util.StreamUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet(name="requestBodyStringServlet", urlPatterns = "/request-body-string")
public class RequestBodyStringServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletInputStream inputStream = request.getInputStream();
        String messagebody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

        System.out.println("messagebody = " + messagebody);

        response.getWriter().write("OK.");

    }
}
inputStream์€ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, request์˜ message body๋ฅผ ๋ฐ”์ดํŠธ์ฝ”๋“œ๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ inputStream์— ์ €์žฅ์ด ๋˜๋Š”๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋ ค๋ฉด Charset์„ UTF-8๋กœ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์—์„œ StreamUtils๋ฅผ ์‚ฌ์šฉํ•ด inputStream์— ์ €์žฅ๋œ ์ŠคํŠธ๋ง์„ messagebody์— utf-8๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

Postman์„ ์ด์šฉํ•œ message body ์ „์†ก
๋‹จ์ˆœ ํ…์ŠคํŠธ Content-type : text/plain

message body๋ฅผ ์ด์šฉํ•ด ๋‹จ์ˆœ TEXT๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” content-type์„ text/plain๋กœ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

 

Send ๊ฒฐ๊ณผ

๊ฒฐ๊ณผ

 

3-2. JSON

JSON ํ˜•์‹ ์ „์†ก.

JSON Content-type : application/json

content-type : application/json

message body : {"name" : "kimjson", "age" : 25}

 

package hello.servlet.basic;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter @Setter @ToString
public class HelloData {
    private String name;
    private int age;
}
package hello.servlet.basic.request;

import com.fasterxml.jackson.databind.ObjectMapper;
import hello.servlet.basic.HelloData;
import org.springframework.util.StreamUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet(name="requestBodyJsonServlet", urlPatterns = "/request-body-json")
public class RequestBodyJsonServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletInputStream inputStream = request.getInputStream();
        String messagebody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        System.out.println("messagebody = " + messagebody);

        HelloData helloData = objectMapper.readValue(messagebody, HelloData.class);
        System.out.println("helloData.getAge() = " + helloData.getAge());
        System.out.println("helloData.getName() = " + helloData.getName());
        System.out.println("helloData = " + helloData);
        response.getWriter().write("OK");
    }
}

 

์ฐธ๊ณ  : JSON ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์‹ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด Jackson, Gson ๊ฐ™์€ JSON ๋ณ€ํ™˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

์Šคํ”„๋ง ๋ถ€ํŠธ๋กœ Spring MVC๋ฅผ ์„ ํƒํ•˜๋ฉด ๊ธฐ๋ณธ์œผ๋กœ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ( ObjectMapper )๋ฅผ ํ•จ๊ป˜ ์ œ๊ณตํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์—์„œ๋Š” ObjectMapper๋ฅผ ์ด์šฉํ•ด json ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด์— ํŒŒ์‹ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Postman์„ ์ด์šฉํ•œ json ํ˜•์‹ ๋ฐ์ดํ„ฐ ์ „์†ก
๊ฒฐ๊ณผ

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ