[Spring] MVC 1편 (4) - HTTP 응닡 데이터

2022. 3. 29. 08:53Β·Spring

πŸ“Œ HttpServletResponse

βœ… μ—­ν• 

  • HTTP μ‘λ‹΅μ½”λ“œ μ§€μ •
  • 헀더, λ°”λ”” 생성
  • 편의 κΈ°λŠ₯ 제곡 : Content-type, μΏ ν‚€, Redirect

 

πŸ“Œ HTTP 응닡 데이터

βœ… μ’…λ₯˜

  • λ‹¨μˆœ ν…μŠ€νŠΈ 응닡
  • HTML 응닡
  • HTTP API - MessageBody JSON 응닡

 

βœ… HTML 응닡

  • content-type: text/html
@Override
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 //Content-Type: text/html;charset=utf-8
 response.setContentType("text/html");
 response.setCharacterEncoding("utf-8");
 
 PrintWriter writer = response.getWriter();
 
 // μ„œλΈ”λ¦Ώ html μ½”λ“œλ₯Ό μžλ°” μ½”λ“œλ‘œ 적어주어야 ν•˜λŠ” λ²ˆκ±°λ‘œμ›€μ΄ 있음
 writer.println("<html>");
 writer.println("<body>");
 writer.println(" <div>μ•ˆλ…•?</div>");
 writer.println("</body>");
 writer.println("</html>");
 }

 

βœ… API JSON 응닡

  • content-type: application/json
private ObjectMapper objectMapper = new ObjectMapper();

 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 //Content-Type: application/json
 response.setHeader("content-type", "application/json");
 response.setCharacterEncoding("utf-8");
 
 HelloData data = new HelloData();
 data.setUsername("kim");
 data.setAge(20);

 //{"username":"kim","age":20}
 String result = objectMapper.writeValueAsString(data);
 response.getWriter().write(result);
 }
  • objectMapper.writeValueAsString() μ‚¬μš©ν•˜λ©΄ 객체λ₯Ό JSON ν˜•μ‹μ˜ 문자둜 λ³€κ²½ν•  수 있음

 


κ°•μ˜ 링크

 

μŠ€ν”„λ§ MVC 1편 - λ°±μ—”λ“œ μ›Ή 개발 핡심 기술 - μΈν”„λŸ° | κ°•μ˜

μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ κ°œλ°œν•  λ•Œ ν•„μš”ν•œ λͺ¨λ“  μ›Ή κΈ°μˆ μ„ κΈ°μ΄ˆλΆ€ν„° μ΄ν•΄ν•˜κ³ , μ™„μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μŠ€ν”„λ§ MVC의 핡심 원리와 ꡬ쑰λ₯Ό μ΄ν•΄ν•˜κ³ , 더 κΉŠμ΄μžˆλŠ” λ°±μ—”λ“œ 개발자둜 μ„±μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€., -

www.inflearn.com

 

μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)

'Spring' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Spring] MVC 1편 (5) - ​ μ„œλΈ”λ¦Ώ, JSPλ₯Ό μ‚¬μš©ν•œ νšŒμ› 관리 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜  (0) 2022.03.29
[Spring] MVC 1편 (3) - HTTP μš”μ²­ 데이터  (0) 2022.03.29
[Spring] MVC 1편 (2) - μ„œλΈ”λ¦Ώ  (0) 2022.03.28
'Spring' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [Spring] MVC 1편 (6) - MVC νŒ¨ν„΄ 적용
  • [Spring] MVC 1편 (5) - ​ μ„œλΈ”λ¦Ώ, JSPλ₯Ό μ‚¬μš©ν•œ νšŒμ› 관리 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜
  • [Spring] MVC 1편 (3) - HTTP μš”μ²­ 데이터
  • [Spring] MVC 1편 (2) - μ„œλΈ”λ¦Ώ
suaring
suaring
개발 곡뢀 둜그
  • suaring
    Sue's devlog
    suaring
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (123)
      • Algorithm (2)
      • WEB (8)
      • Spring (26)
      • Java (83)
      • Kotlin (1)
      • Database (1)
      • Infra (0)
      • Git (1)
      • devlog (1)
  • 인기 κΈ€

  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
suaring
[Spring] MVC 1편 (4) - HTTP 응닡 데이터
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”