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๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด