BootCamp 41day

GyeongNamΒ·2024λ…„ 1μ›” 15일
0

BootCamp

λͺ©λ‘ 보기
35/49
post-thumbnail

πŸ“… 2024λ…„ 01μ›” 11일


41일차 : Spring (1)

java http 톡신

public class WebServer1 {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            System.out.println("8081 μ„œλΉ„μŠ€ μ‹œμž‘");
            while (true) {
                try (Socket socket = serverSocket.accept()){
                    String httpRes = "HTTP/1.1 200 OK \r\n\r\n" + "hello world";
                    socket.getOutputStream().write(httpRes.getBytes("UTF-8"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class WebServer2 {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            System.out.println("8081 μ„œλΉ„μŠ€ μ‹œμž‘");
            while (true) {
                // socket κ°μ²΄λŠ” μ‚¬μš©μžμ˜ ν΄λΌμ΄μ–ΈνŠΈ 객체닀.
                try (Socket socket = serverSocket.accept()){
                    String httpRes =
                            "HTTP/1.1 200 OK \r\n" +
                            "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
                            "<html>" +
                                "<body>" +
                                    "<h1> Hello World </h1>"+
                                    "<p> μ•ˆλ…•ν•˜μ„Έμš” μ—¬λŸ¬λΆ„ </p>"+
                                "</body>"+
                            "</html>"
                            ;
                    socket.getOutputStream().write(httpRes.getBytes(StandardCharsets.UTF_8));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class WebServer3 {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            System.out.println("8081 μ„œλΉ„μŠ€ μ‹œμž‘");
            while (true) {
                try (Socket socket = serverSocket.accept()){
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while (!(line = br.readLine()).isBlank()){
                        sb.append(line+"\n");
                    }
                    if(sb.toString().contains("POST")){
                        char[] buffer = new char[1024];
                        br.read(buffer);
                        System.out.println("POST body data : " + new String(buffer));
                        System.out.println("POST all data : " + sb);
                    }
                    String httpRes = "HTTP/1.1 200 OK \r\n\r\n" + "ok";
                    socket.getOutputStream().write(httpRes.getBytes("UTF-8"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Spring Http Get 톡신

@Controller
@RequestMapping("hello")
public class HelloController {
    /*
     @RequestBody κ°€ μ—†κ³  return νƒ€μž…μ΄ String 이면 templates의 html 파일 return
     data λ§Œμ„ return ν• λ•ŒλŠ” @ResponseBodyλ₯Ό 뢙인닀.
     @
     */
    @GetMapping("String")
    @ResponseBody
    public String helloString(){
        return "hello_String";
    }
    @GetMapping("screen")
    public String helloScreen(){
        return "screen";
    }
    @GetMapping("screen-model")
    public String helloScreenModel1(Model model){
        /*
        화면에 data λ₯Ό λ„˜κΈ°κ³  싢을 λ•Œ model 객체λ₯Ό μ‚¬μš©
        λͺ¨λΈμ— key:value ν˜•μ‹μœΌλ‘œ 전달
         */
        model.addAttribute("data", "ν•œν¬μ€€");
        return "screen";
    }
    @GetMapping("screen-model-RequestParam")
    @ResponseBody
    public String helloScreenModel2(@RequestParam(value = "name") String inputName, Model model){
        model.addAttribute("data", inputName);
        return "screen";
    }
    /*
    @PathVariable 방식은 urlλ₯Ό 톡해 μžμ›μ˜ ꡬ쑰λ₯Ό λͺ…ν™•ν•˜κ²Œ
    ν‘œν˜„ν•  수 μžˆμ–΄, 쒀더 RestFul API에 적합.
     */
    @GetMapping("screen-model-PathVariable/{name}")
    public String helloScreenModel3(@PathVariable String name, Model model){
        model.addAttribute("data", name);
        return "screen";
    }
}

Spring μ‹€μŠ΅ github 링크

profile
503 Service Unavailable Error

0개의 λŒ“κΈ€