DevLog ๐Ÿ˜ถ

[Spring] ์„œ๋ธ”๋ฆฟ์ด๋ž€? HttpServletRequest, HttpServletResponse ๋ณธ๋ฌธ

Back-end/Spring

[Spring] ์„œ๋ธ”๋ฆฟ์ด๋ž€? HttpServletRequest, HttpServletResponse

dolmeng2 2022. 8. 10. 17:01

- ๊น€์˜ํ•œ ๋‹˜์˜ '์Šคํ”„๋ง MVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ '์„ ๋ณด๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค ๐Ÿ˜Š

 

์Šคํ”„๋ง MVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ  - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ์›น ๊ธฐ์ˆ ์„ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ดํ•ดํ•˜๊ณ , ์™„์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํ”„๋ง MVC์˜ ํ•ต์‹ฌ ์›๋ฆฌ์™€ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜๊ณ , ๋” ๊นŠ์ด์žˆ๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ์„ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค., -

www.inflearn.com


- ์ง€๋‚œ ํฌ์ŠคํŒ…๊ณผ ์ด์–ด์ง‘๋‹ˆ๋‹ค :D

 

[Spring] WAS, ์„œ๋ธ”๋ฆฟ, ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ์™€ ์Šค๋ ˆ๋“œ ํ’€

- ๊น€์˜ํ•œ ๋‹˜์˜ '์Šคํ”„๋ง MVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ '์„ ๋ณด๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค ๐Ÿ˜Š ์Šคํ”„๋ง MVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ  - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ์›น ๊ธฐ

cl8d.tistory.com

 

- ์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์›น ๊ธฐ์ˆ ์— ํ•„์š”ํ•œ ํ•ต์‹ฌ ๊ฐœ๋…๋“ค์„ ์•Œ์•„๋ณด์•˜๋‹ค.

- ์ด๋ฒˆ์—๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ ํ™˜๊ฒฝ์—์„œ ์ง์ ‘ ์„œ๋ธ”๋ฆฟ์„ ๋“ฑ๋กํ•˜๊ณ  ์‚ฌ์šฉํ•ด๋ณด์ž.

 

| ์„œ๋ธ”๋ฆฟ (Servlet)

- ๊ธฐ๋ณธ์ ์œผ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ๊ฒฝ์šฐ ํ†ฐ์บฃ ์„œ๋ฒ„๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ช‡ ๊ฐ€์ง€ ์ œ๊ณตํ•œ๋‹ค.

[SpringStudyApplication.java] - ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ํŒŒ์ผ

@ServletComponentScan
@SpringBootApplication
public class SpringStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringStudyApplication.class, args);
    }

}

- ๋‹ค์Œ๊ณผ ๊ฐ™์ด @ServletComponentScan์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

[HelloServlet.java]

@WebServlet(name="helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet.service");
        System.out.println("request = " + req);
        System.out.println("response = " + resp);

        // paramter ์„ค์ •
        String username = req.getParameter("username");
        System.out.println("username = " + username);

        // Response
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write("hello " + username);
    }
}

- @WebServlet์„ ํ†ตํ•ด ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„์„ ๋ถ™์ด๊ณ , urlPattern์„ ํ†ตํ•ด url ๋งคํ•‘์„ ํ•ด์ค€๋‹ค.

- ์œ„ ์˜ˆ์ œ์—์„œ๋Š” /hello๋ผ๋Š” ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์™”์„ ๋•Œ ์‹คํ–‰๋œ๋‹ค.

http://localhost:8080/hello?username=hi

 

getParameter๋ฅผ ํ†ตํ•ด์„œ, ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋“ค์–ด์˜จ ๊ฐ’์„ ํŒŒ์‹ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„ ๊ฒฝ๋กœ๋กœ ์š”์ฒญํ•˜๊ฒŒ ๋˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™”๋ฉด์— ์ถœ๋ ฅ๋œ๋‹ค.

- ์ด๋Š”, response.getWriter().write๋ฅผ ํ†ตํ•ด์„œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด์ค€ ๋ถ€๋ถ„์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (HttpMessageBody์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฑฐ) 

์ฝ˜์†”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐํžˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ์–ธ๊ธ‰ํ•˜์˜€๋˜ connector๋ฅผ ํ†ตํ•ด HttpServletRequest์™€ HttpServletResponse๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ๋กœ, RequestFacade, ResponseFacade์˜ ๊ฒฝ์šฐ org.apache.catalina.connector.Request/Response์˜ ๊ตฌํ˜„์ฒด์ด๋‹ค. (์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด)

Facade๋Š” ๋””์ž์ธ ํŒจํ„ด์—์„œ๋„ ์“ฐ์ด๋Š”๋ฐ, ๋‚ด๋ถ€์˜ ๊ตฌ์กฐ๋ฅผ ์ˆจ๊ธธ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ๊ฐ„๋žตํ™”๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ.

๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋ฐ›์€ ์‹ค์ œ Request/Response์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

 

๐Ÿšฉ ์ฆ‰, ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ๋‚ด์žฅ ํ†ฐ์บฃ ์„œ๋ฒ„๋ฅผ ๊ฐ€๋™ํ•˜๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ฒŒ ๋˜๊ณ ,

ํ˜„์žฌ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด helloServlet ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ์ƒํƒœ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ดํ›„, HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€ (GET /hello?username=hi)๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ HttpServletRequest/Response๋ฅผ ์ƒ์„ฑํ•˜๊ณ ,

helloServlet์„ ์‹คํ–‰ํ•ด์ค€ ๋‹ค์Œ (service()) ๋“ฑ๋ก๋œ HttpServletResponse ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ HTTP ์‘๋‹ต์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

 


| HttpServletRequest 

- ์„œ๋ธ”๋ฆฟ์€ HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋Œ€์‹  HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒ์‹ฑํ•ด์ค€๋‹ค.

    - ๊ทธ๋ž˜์„œ get() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์›ํ•˜๋Š” ๋‚ด์šฉ (ํ—ค๋”, ๋ฐ”๋”” ๋“ฑ...)์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

- ๋˜ํ•œ, HTTP ์š”์ฒญ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ์œ ์ง€๋˜๋Š” ์ž„์‹œ ์ €์žฅ์†Œ ๊ธฐ๋Šฅ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

    - (์ €์žฅ) request.setAttribute(name, value) <-> (์กฐํšŒ) request.getAttribute(name)

- ํ˜น์€, ์„ธ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•ด์ค€๋‹ค.

    - request.getSession(create: true) --> ์ฑ„ํŒ…๋ฐฉ ๊ฐ™์€ ๊ธฐ๋Šฅ์—์„œ ์„ธ์…˜์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์“ด๋‹ค...!

 

[๋งŽ์ด ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™์€ start-line ์ •๋ณด ํŒŒ์‹ฑ]

http://localhost:8080/request-header?username=hi

request.getMethod() GET
request.getProtocol() HTTP/1.1
request.getScheme() http
request.getRequestURL(); http://localhost:8080/request-header
request.getRequestURI(); /request-header
request.getQueryString() username=hi
request.isSecure() false (https ์—ฌ๋ถ€)

 

โญ Http ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ -> ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ

โœ” GET - ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ

- /hello?username=hi&age=22

- ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , URL์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

ex) ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง•์—์„œ ๋งŽ์ด ์‚ฌ์šฉ

 

- HttpServletRequest๋ฅผ ํ†ตํ•ด์„œ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒํ•˜๊ธฐ

ํ˜•์‹ ๋ฆฌํ„ด ํƒ€์ž… ์„ค๋ช…
request.getParameter("username") String ๋‹จ์ผ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ
request.getParameterNames() Enumeration<String> ํŒŒ๋ผ๋ฏธํ„ฐ ๋„ค์ž„ ๋ชจ๋‘ ์กฐํšŒ
request.getParameterMap() Map<String, String[]> Map ํ˜•์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ
request.getParameterValues("username") String[] ๋™์ผ ์ด๋ฆ„์˜ ๋ณต์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ

- ์ฐธ๊ณ ๋กœ, ๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์กด์žฌํ–ˆ์„ ๋•Œ getParameter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

โœ” POST - HTML Form 

- content-type: application/x-www-form-urlencoded

- ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ• (username=hi&age=22)

ex) ํšŒ์› ๊ฐ€์ž…, ์ƒํ’ˆ ์ฃผ๋ฌธ, HTML form ์‚ฌ์šฉ (form action="" method="post")

 

- ์ฐธ๊ณ ๋กœ, GET ๋ฐฉ์‹์ด๋ž‘ ํ˜•์‹์ด ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— request.getParamter()๋กœ ๋™์ผํ•˜๊ฒŒ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

- ๋‹จ, GET ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— content-type์ด ์—†๊ณ , 

POST HTML form ๋ฐฉ์‹์€ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ์ „์†กํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์— content-type์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

โœ” HTTP message body์— ์ง์ ‘ ๋‹ด๊ธฐ

- HTTP API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.

ex) JSON, XML, TEXT

- ์ฃผ๋กœ JSON์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. 

- POST, PUT, PATCH ๋ฐฉ์‹

@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream inputStream = req.getInputStream();
        String messageBody = StreamUtils.copyToString(inputStream,
                StandardCharsets.UTF_8);
        System.out.println("messageBody = " + messageBody);
        resp.getWriter().write("ok");
    }
}

- ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ text ํƒ€์ž…์„ ์ด์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Postman์—์„œ ํƒ€์ž…์„ raw->text ํƒ€์ž…์œผ๋กœ ํ•˜๊ณ  ์ „์†กํ•˜๋ฉด ๋œ๋‹ค.

- inputStream์„ ํ†ตํ•ด์„œ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋ฐ”๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

- ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ string์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ StreamUtils.copyToString์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ธ์ฝ”๋”ฉ ํƒ€์ž…์€ UTF_8์ด๋‹ค.

 

- ๋‹ค์Œ์œผ๋กœ, ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ JSON ํ˜•์‹์„ ์•Œ์•„๋ณด์ž.

- content-type์„ raw -> application/json์œผ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

- messageBody์—๋Š” {"username:" "hi", "age": 22} ์œผ๋กœ ์ž‘์„ฑํ•ด์ค€๋‹ค.

- ํŒŒ์‹ฑ์„ ์œ„ํ•œ ๊ฐ์ฒด ํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•˜์ž.

 

[HelloData.java]

@Data
public class HelloData {
    private String username;
    private int age;
}

- Setter๋Š” ๊ผญ ์—ด์–ด์ค˜์•ผ ํ•œ๋‹ค. (๊ทธ๋ž˜์„œ ์›ฌ๋งŒํ•˜๋ฉด ์‹ค์ œ ๊ฐœ๋ฐœ์—์„œ๋Š” DTO๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค)

 

๐Ÿšฉ ์ด๋ฒˆ์— ์ฐพ์•„๋ณด๋ฉด์„œ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ObjectMapper๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด setter๊ฐ€ ์—†์–ด๋„ ํ•„๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค!

- ์ฐพ์•„๋ดค๋Š”๋ฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ @Getter๋‚˜ @Setter ์ค‘์— ํ•˜๋‚˜๋งŒ ์žˆ์–ด๋„ ๋™์ž‘ํ•œ๋‹ค.

    - objectMapper๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ•„๋“œ๋ช…์œผ๋กœ ๋งค์นญํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, getter์™€ setter๋ฅผ ํ†ตํ•ด์„œ ๋งค์นญํ•ด์ค€๋‹ค!!

- @Setter๊ฐ€ ํ•„์š”์—†๋Š” ์ด์œ ๋Š”, BeanDeserializer -> deserializeAndSet ๋ฉ”์„œ๋“œ์—์„œ reflection์„ ํ†ตํ•ด ๊ฐ’์„ ์ฃผ์ž…ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค...!)

    - ๋ฆฌํ”Œ๋ž™์…˜์€ ๋Ÿฐํƒ€์ž„์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋‹ˆ๊นŒ, ๊ตณ์ด @Setter๋ฅผ ํ†ตํ•ด ๋ฏธ๋ฆฌ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฑฐ.

(์ฐธ๊ณ : ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ๋งํฌ! :D)

 

@RequestBody์— ์™œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋Š” ํ•„์š”ํ•˜๊ณ , Setter๋Š” ํ•„์š” ์—†์„๊นŒ? #3

์ด์ „ ๊ธ€์—์„œ๋Š” RestController์—์„œ @RequestBody ๋ฐ”์ธ๋”ฉ์„ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ObjectMapper๊ฐ€ ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.๊ทธ๋ฆฌ๊ณ  RequestBody๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, DTO๊ฐ€ Property๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๊ฑฐ๋‚˜ Delegate๋ฅผ ํ•œ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ

velog.io

- ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— objectMapper๋ฅผ ์‚ฌ์šฉํ•˜๋Š” @RequestBody์—์„œ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ DTO์— @Setter๋ฅผ ์•ˆ ๋ถ™์—ฌ์ค˜๋„ ๋œ๋‹ค...!

- @Getter๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•œ๋‹ค๋ฉด public, @Setter๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด private~public ๋ชจ๋“  ์ ‘๊ทผ์ง€์ •์ž๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

 

@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream inputStream = req.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.getUsername() = " + helloData.getUsername());
        System.out.println("helloData.getAge() = " + helloData.getAge());

        resp.getWriter().write("ok");
    }
}

- ์•„๋ฌดํŠผ, ๋‹ค์‹œ ๋ณธ๋ก ์œผ๋กœ ๋Œ์•„์™€์„œ...

- Jackson์˜ ObjectMapper๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค. ๋ณดํ†ต์€ Bean์œผ๋กœ ๋“ฑ๋กํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ๋Š” ํ…Œ์ŠคํŠธ๋‹ˆ๊นŒ ๊ทธ๋ƒฅ ๋ฐ”๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.

- messageBody์— ๋‹ด๊ธด ๋ฐ์ดํ„ฐ๋ฅผ HelloData๋ผ๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. 

- ์‹คํ–‰ํ•ด๋ณด๋ฉด getUsername, getAge๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋ฐ›์•„์ง„ ๊ฑธ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค :)

 


| HttpServletResponse

- HttpServletResponse์˜ ๊ฒฝ์šฐ HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

- HTTP response Code, Header, Body๋ฅผ ์ƒ์„ฑํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

- ํ˜น์€, Content-type์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์ฟ ํ‚ค ์ƒ์„ฑ, Redirect ๊ฐ™์€ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•œ๋‹ค!

 

[์‘๋‹ต ์ฝ”๋“œ ์ง€์ •]

response.setStatus(HttpServletResponse.SC_OK); // 200

 

[ํ—ค๋” ์ง€์ •]

response.setHeader("Content-Type", "text/plain;charset=utf-8")

 

[๋ฐ”๋”” ์ง€์ •]

response.getWriter().println("ok")

 

[content-type ํŽธ์˜ ๊ธฐ๋Šฅ] - ํ—ค๋”์—์„œ ์ง€์ •ํ•œ Content-Type์„ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์ง€์ •

response.setContentType("text/plain");

response.setCharacterEncoding("utf-8");

response.setContentLength(2)" // default๋กœ 2.

 

[์ฟ ํ‚ค ํŽธ์˜ ๊ธฐ๋Šฅ] - Set-Cookie : myCookie=hello; Max-Age=600

Cookie cookie = new Cookie("myCookie", "hello");

cookie.setMaxAge(600);

response.addCookie(cookie);

 

[redirect ํŽธ์˜ ๊ธฐ๋Šฅ] response.setStatus(HttpServletResponse.SC_FOUND); // 302

- redirectresponse.setHeader("Location", "/hello.html"); 

// ์œ„์˜ ๋‘ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ค€ ๊ธฐ๋Šฅ

response.sendRedirect("/hello.html");

 

 

โญ HTML ์‘๋‹ต ๋ฐ์ดํ„ฐ

โœ” ํ…์ŠคํŠธ๋กœ ์‘๋‹ตํ•˜๊ธฐ

writer.println("hi");

 

โœ” HTML๋กœ ์‘๋‹ตํ•˜๊ธฐ

@WebServlet(name = "responseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");

        PrintWriter writer = resp.getWriter();
        writer.println("<html>");
        writer.println("<body>");
        writer.println(" <div>์•ˆ๋…•?</div>");
        writer.println("</body>");
        writer.println("</html>");
        resp.getWriter().write("ok");
    }
}

- ์ƒ๋‹นํ•œ ํ•˜๋“œ์ฝ”๋”ฉ์ด์ง€๋งŒ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ •์  html ์‘๋‹ต๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. (ํ•˜์ง€๋งŒ ์ด๊ฑธ ์“ฐ๋Š” ์ผ์€ ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค...)

 

 

โœ” JSON์œผ๋กœ ์‘๋‹ตํ•˜๊ธฐ 

@WebServlet(name = "responseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "application/json");
        resp.setCharacterEncoding("utf-8");

        HelloData data = new HelloData();
        data.setUsername("hi");
        data.setAge(22);

        String result = objectMapper.writeValueAsString(data);
        resp.getWriter().write(result);
    }
}

- content-type์„ json์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. ๊ฐ์ฒด์— setter๋ฅผ ํ†ตํ•ด ๋„ฃ์–ด์ฃผ๊ณ , objectMapper๋ฅผ ํ†ตํ•ด์„œ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

 

- ์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธ์ ์ธ httpServletRequest, httpServletResponse์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

- ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ณธ๊ฒฉ์ ์ธ ํšŒ์› ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ์ •์˜ํ•˜์—ฌ JSP, MVC ํŒจํ„ด์„ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋‹ค๋ฃจ์–ด๋ณด๊ฒ ๋‹ค.

Comments