๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿƒ Spring

Spring | REST API ์ˆ™์ œ

  • -

KAKAO open REST API ๋ฅผ Java ๋กœ ๋‹ค๋ค„๋ณด๊ธฐ ์œ„ํ•œ ๊ณต๋ถ€ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์ผ๋ถ€ ๋‚ด์šฉ์—๋Š” ๊ฐ•์˜๋กœ ๊ณต๋ถ€ ์ค‘์ด์˜€๋˜ ๋‚ด์šฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. (ํŒจ์ŠคํŠธ์บ ํผ์Šค)

 

๊ฐœ์š”

์ฑ… ๊ฒ€์ƒ‰ API ๋ฅผ ์ด์šฉํ•ด์„œ ํ‚ค์›Œ๋“œ ์ž…๋ ฅ ์‹œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

 

๊ตฌ์„ฑ

bookHome.jsp

์ฑ… ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ์ž…๋ ฅํผ

 

BookController.java

"/book" | GET

  • @ModelAttribute
๋”๋ณด๊ธฐ
@Controller
public class BookController extends HttpServlet {

	/**
	 * @param model
	 * @param result
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/book", method = RequestMethod.GET)
	public String getBookHome(Model model,
    		@ModelAttribute("listMap") JsonRootBean result) throws IOException {

		if (result != null) {
			model.addAttribute("listMap", result);
		}
		return "bookHome";
	}
}

 

"/book" | POST

  • kakao REST API
  • ObjectMapper
  • StringBuilder
  • BufferedReader
  • HttpURLConnection
๋”๋ณด๊ธฐ
@Controller
public class BookController extends HttpServlet {

        /**
         * @param req
         * @param model
         * @param rttr
         * @return
         * @throws IOException
         */
	@RequestMapping(value = "/book", method = RequestMethod.POST)
	public String postBookHome(HttpServletRequest req, Model model,
    		RedirectAttributes rttr) throws IOException {

                // get ์ฑ… ์ด๋ฆ„
                String sbName = req.getParameter("sbname");
                // ํ•œ๊ธ€ ์ธ์ฝ”๋”ฉ | url ์˜์–ด๋Š” ๋ฐ”๋กœ ์ ์šฉ๋˜์ง€๋งŒ, ํ•œ๊ธ€์€ ์ธ์ฝ”๋”ฉ์ด ํ•„์š”ํ•˜๋‹ค.
                String bookName = URLEncoder.encode(sbName, "UTF-8");
                
                // 'REST API URL' + 'query' = ์ฑ… ์ด๋ฆ„
                URL url = new URL("https://dapi.kakao.com/v3/search/book?query=" + bookName);
                // Kakao dev rest api ๋ฐœ๊ธ‰
                String kakao_apikey = "{REST API Key}";

                // Custom Serialize
                ObjectMapper mapper = new ObjectMapper();
                // 
                StringBuilder sb = new StringBuilder();
                // 
                BufferedReader br;

		try {
        
			HttpURLConnection con = (HttpURLConnection) url.openConnection();

			// Request Header ์ •์˜
			con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			con.setRequestProperty("Authorization", "KakaoAK " + kakao_apikey);

			// ์ „์†ก๋ฐฉ์‹
			con.setRequestMethod("GET");

			// ์„œ๋ฒ„์— ์—ฐ๊ฒฐ๋˜๋Š” Timeout ์‹œ๊ฐ„ ์„ค์ •
			con.setConnectTimeout(5000);

			// InputStream ์ฝ์–ด ์˜ค๋Š” Timeout ์‹œ๊ฐ„ ์„ค์ •
			con.setReadTimeout(5000);

			// URLConnection์— ๋Œ€ํ•œ doOutput ํ•„๋“œ๊ฐ’์„ ์ง€์ •๋œ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
			// URL ์—ฐ๊ฒฐ์€ ์ž…์ถœ๋ ฅ์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.
			// URL ์—ฐ๊ฒฐ์„ ์ถœ๋ ฅ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ DoOutput ํ”Œ๋ž˜๊ทธ๋ฅผ true๋กœ ์„ค์ •ํ•˜๊ณ ,
			// ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” false๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ false์ด๋‹ค.
			con.setDoOutput(true);

			if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            
				br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
				// json ํ˜•ํƒœ๋กœ ๋งŒ๋“ค๊ธฐ
                		String line;
				while ((line = br.readLine()) != null) {
					sb.append(line).append("\n");
				}
				br.close();
                
				BookInfoVO sbList = mapper.readValue(sb.toString(), BookInfoVO.class);
				rttr.addFlashAttribute("listMap", sbList); // "listMap" Parameter ์ „๋‹ฌ
                
			} else {
            
				model.addAttribute("error", con.getResponseMessage());
			}
		} catch (Exception e) {
        
			System.err.println(e.toString());
		}
        
		return "redirect:/book"; // Parameter ์ „๋‹ฌ ๊ฒฝ๋กœ
	}
}

 

API

HTTP Method(GET/POST) ENDPOINT ์„ค๋ช… ์†์„ฑ
GET /book ํ™”๋ฉด  
POST /book REST API ๊ฒ€์ƒ‰
๊ฒฐ๊ณผ ์ „๋‹ฌ
 

 

RESULT

JSON ํƒ€์ž… ํ˜•ํƒœ๋กœ ๊ฐ’ ๋ฐ›๊ธฐ

 

'๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก > ๐Ÿƒ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring | Exception  (0) 2023.01.19
Spring | RedirectAttributes  (0) 2023.01.17
Spring MVC | Web Application  (0) 2023.01.13
Spring Boot/VSC | New Project  (0) 2023.01.10
Spring/Mybatis | foreach(bulk insert)  (0) 2023.01.09
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.