라우터

Jan 22, 2024
라우터

notion image
 
자원명이 아니라 식별자 사용 - 파싱
http://bank.com/account GET : 달라는 것(select) http://bank.com/account/10 GET : 달라는 것(select) http://bank.com/account POST : 주는 것(insert) + 쿼리 더 받아야 함/바디 데이터가 필요함 http://bank.com/account/1 DELETE : 삭제하는 것(delete) http://bank.com/account/1 PUT : 수정하는 것(update) + 쿼리 더 받아야 함/바디 데이터가 필요함
 
요청 시에 URL을 사용함
사용자가 DB에 직접적이 아니라 자바에 요청하는 이유 : 보안
get : 요청 시에는 body가 필요 없음
데이터를 응답 받을 때는 body가 필요함 - reopnseBody + header에 mime type이 필요함
식별자 요청을 해서 쿼리에 DB해서 데이터를 받음
post, update : 요청 시에 body가 필요함
응답 받을 때 body 데이터가 아닌 http 코드 200만 주면 됨
 
write 요청은 돌려줄 것이 없음
get, delete는 body가 없음
post, put은 요청 body가 있음
 
WAS : 알아서 파싱 다 해줌
 
주소?body데이터
?로 구분
 
1) GET과 POST문
import dao.BankDAO; import model.Account; import java.util.List; import java.util.Scanner; public class BankApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // http://bank.com/account GET // http://bank.com/account/10 GET // http://bank.com/account POST // http://bank.com/account/1 DELETE // http://bank.com/account/1 PUT System.out.println("http 메서드를 입력하세요"); String method = sc.nextLine(); System.out.println("식별자를 입력하세요"); String action = sc.nextLine(); String body = ""; BankDAO bankDAO = new BankDAO(); if(method.equals("GET")){ if(action.equals("/account")){ List<Account> accountList = bankDAO.selectAll(); System.out.println(accountList); }else if(action.equals("/account/1")){ Account account = bankDAO.selectByNumber(1); System.out.println(account); } }else if(method.equals("POST")){ System.out.println("body 데이터를 입력하세요"); body = sc.nextLine(); // password=1234&balance=1000 String[] st1 = body.split("&"); String password = st1[0].split("=")[1]; int balance = Integer.parseInt(st1[1].split("=")[1]); if(action.equals("/account")){ bankDAO.insertByNumber(password,balance); } }else if(method.equals("PUT")){ }else if(method.equals("DELETE")){ } } }
notion image
notion image
notion image
 
2) PUT문
import dao.BankDAO; import model.Account; import java.util.List; import java.util.Scanner; public class BankApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // http://bank.com/account GET // http://bank.com/account/10 GET // http://bank.com/account POST // http://bank.com/account/1 DELETE // http://bank.com/account/1 PUT System.out.println("http 메서드를 입력하세요"); String method = sc.nextLine(); System.out.println("식별자를 입력하세요"); String action = sc.nextLine(); String body = ""; BankDAO bankDAO = new BankDAO(); if(method.equals("PUT")){ System.out.println("body 데이터를 입력하세요"); int balance = sc.nextInt(); if (action.equals("/account/1")) { int result = bankDAO.updateByNumber(balance, 1); } } } }
notion image
 
3) DELET문
import dao.BankDAO; import model.Account; import java.util.List; import java.util.Scanner; public class BankApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // http://bank.com/account GET // http://bank.com/account/10 GET // http://bank.com/account POST // http://bank.com/account/1 DELETE // http://bank.com/account/1 PUT System.out.println("http 메서드를 입력하세요"); String method = sc.nextLine(); System.out.println("식별자를 입력하세요"); String action = sc.nextLine(); String body = ""; BankDAO bankDAO = new BankDAO(); if(method.equals("DELETE")){ if (action.equals("/account/1")) { int result = bankDAO.deleteByNumber(1); } } } }
notion image
 
Share article

More articles

See more posts
RSSPowered by inblog