ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Project/Instagram

[SpringBoot]

yeahajeong 2020. 7. 15. 17:52
๋ฐ˜์‘ํ˜•

  Hello Controller ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ  

ํŒจํ‚ค์ง€์™€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (์ผ๋ฐ˜์ ์ธ ํŒจํ‚ค์ง€๋ช…์€ ์›น ์‚ฌ์ดํŠธ ์ฃผ์†Œ์˜ ์—ญ์ˆœ์œผ๋กœ ์ž‘์„ฑ ) ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•˜๋Š”๋ฐ import๋ฅผ ์ณ๋„ ์ž๊พธ ์ง€์›Œ์ ธ์„œ ๋„ˆ๋ฌด๋‚˜ ๋‹นํ™ฉํ–ˆ๋‹ค. ์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ์•ˆ์จ๋ดค์œผ๋‹ˆ ๋ชจ๋ฅด์ง€.. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” import๋Š” ์ž๋™์œผ๋กœ ์ง€์›Œ์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ๊ฑฐ์˜€๋‹ค ๊ทธ๋ƒฅ import๋ง๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋˜๋Š”๊ฑฐ์˜€๋‹ค; ์ข‹๊ตฐ

 

//Application ํด๋ž˜์Šค๋Š” ์•ž์œผ๋กœ ๋งŒ๋“ค ํ”„๋กœ์ ํŠธ์˜ ๋ฉ”์ธ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
package com.yeahajeong.hastagram;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@SpringBootApplication์œผ๋กœ ์ธํ•ด ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ์ž๋™์„ค์ •, ์Šคํ”„๋ง Bean์ฝ๊ธฐ์™€ ์ƒ์„ฑ์„ ๋ชจ๋‘ ์ž๋™์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
//ํŠนํžˆ๋‚˜ @SpringBootApplication์ด ์žˆ๋Š” ์œ„์น˜๋ถ€ํ„ฐ ์„ค์ •์„ ์ผ๊ฑฐ๊ฐ€๊ธฐ ๋–„๋ฌธ์— ์ด ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ ํ”„๋กœ์ ํŠธ ์ƒ๋‹จ์— ์œ„์ฑ„ํ–์•ผํ•จ
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        //SpringApplication.run ์œผ๋กœ ์ธํ•ด ๋‚ด์žฅ WAS(์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„)๋ฅผ ์‹คํ–‰
        //๋‚ด์žฅ WAS๋ž€ ๋ณ„๋„๋กœ ์™ธ๋ถ€์— WAS๋ฅผ ๋‘์ง€ ์•Š๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•  ๋•Œ ๋‚ด๋ถ€์—์„œ WAS๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ
        SpringApplication.run(Application.class, args);
        //์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ญ์ƒ ์„œ๋ฒ„์— ํ†ฐ์บฃ์„ ์„ค์น˜ํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  ์Šคํ”„๋ง ๋ถ€ํŠธ๋กœ ๋งŒ๋“ค์–ด์ง„ JarํŒŒ์ผ๋กœ ์‹คํ–‰ํ•˜๋ฉด ๋จ
    }
        
}

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” ์–ธ์ œ ์–ด๋””์„œ๋‚˜ ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด์žฅ AWS์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. ์™ธ์žฅ WAS๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ชจ๋“  ์„œ๋ฒ„๋Š” WAS์˜ ์ข…๋ฅ˜์™€ ๋ฒ„์ „, ์„ค์ •์„ ์ผ์น˜์‹œ์ผœ์•ผํ•˜๋‹ˆ๊นŒ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

 

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ Controller ์ƒ์„ฑํ•œ ํ›„ ๊ฐ„๋‹จํ•œ API๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค.

package com.yeahajeong.hastagram.web;

import org.springframework.web.bind.annotation.RestController;

//@RestController
//์ปจํŠธ๋กค๋Ÿฌ๋ฅผ JSON์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋งŒ๋“ค์–ด์คŒ
//์˜ˆ์ „์—๋Š” @RequestBody๋ฅผ ๊ฐ ๋ฉ”์†Œ๋“œ๋งˆ๋‹ค ์„ ์–ธํ–ˆ๋˜๊ฒƒ์„ ํ•œ๋ฒˆ์— ํ•ด์ฃผ๋Š” ๊ฒƒ
@RestController
public class HelloController {

    //@GetMapping
    //HTTP Method์ธ Get์˜ ์š”์ฒญ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” API๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    //์˜ˆ์ „์—๋Š” @RequestMapping(method = RequestMethod.GET)์œผ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.
    //์ด์ œ ์ด ํ”„๋กœ์ ํŠธ๋Š” /hello๋กœ ์š”์ฒญ์ด ์˜ค๋ฉด ๋ฌธ์ž์—ด hello๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„๋‹ค.
    public String hello() {
        return "hello";

    }
}

 

์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณธ๋‹ค. WAS๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ๊ฒ€์ฆํ•ด๋ณธ๋‹ค. java์—์„œ ๋งŒ๋“  ํŒจํ‚ค์ง€ ๊ทธ๋Œ€๋กœ test์—์„œ๋„ ๋งŒ๋“ ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๋Š” ๋Œ€์ƒ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์— Test๋ฅผ ๋ถ™์ธ๋‹ค.

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€