ÃֽŠ°Ô½Ã±Û(JAVA)
2019.05.29 / 24:18

°ü±¤Á¤º¸ ¼­ºñ½º ¿ÀÇ API È°¿ëÇϱâ(Spring, JSON, AJAX)

Źµ¹ÀÌ°³¹ßÀÚ
Ãßõ ¼ö 231

°ü±¤Á¤º¸ API¸¦ È°¿ëÇØ ÇÁ·ÎÁ§Æ®¸¦ ÁøÇàÇϸ鼭 ¿ÀÇ API¸¦ óÀ½ ´Ù·ïº¸°Ô µÇ¾ú½À´Ï´Ù.

µû·Î ¹è¿ìÁöµµ ¾ÊÀºÅÍ¶ó ±¸±Û¸µÀ» ¿­½ÉÈ÷ ÇØ°¡¸é ¹è¿ü¾î¿ä.

±× Áß¿¡¼­ Á¦°¡ ÀÌÇØÇϱ⠽¬¿ü´ø°Ç ¾Æ·¡ ºí·Î±×¿´¾î¿ä.

http://shlee0882.tistory.com/2

¿©±â¿¡ ÀÚ¹Ù Äڵ带 ÆÄÀÏ·Î ¿Ã·ÁÁּż­ ±×°É ¶â¾îº¸°í Çϸç API¸¦ È°¿ëÇÒ ¼ö ÀÖ¾ú½À´Ï´Ù.



¿ÀÇ API, °ø°ø µ¥ÀÌÅ͸¦ ¹Þ¾Æº¸ÀÚ(Spring, JSON, AJAX)


ÀÚ½ÅÀÌ ¾î¶² µ¥ÀÌÅÍ°¡ ÇÊ¿äÇÑÁö »ý°¢Çغ¸°í, ¸Þ´º¾óÀ» º¸¸é¼­ ¿øÇÏ´Â µ¥ÀÌÅ͸¦ Ãß·Á³¾ ¹æ¹ýÀ» ¸ð»öÇÏ´Â°Ô ÁÁ°Ú¾î¿ä.

Á¦°¡ ÇÊ¿äÇÑ°Ç Tour API(°ü±¤Á¤º¸ ¼­ºñ½º API)¿¡¼­ Àå¼Òº° ±¸Ã¼ÀûÀÎ ³»¿ëÀ̾ú½À´Ï´Ù.


<API È°¿ë ¸Þ´º¾ó ÀϺÎ>

Ç׸ñ ±¸ºÐÀ» º¸¸é Çʼö(1)À̶ó°í ÀûÇôÀÖÁÒ

¿äû ¸Þ½ÃÁö, Áï, URL¿¡ Àú Ç׸ñÀº ²À µé¾î°¡¾ßÁö °á°ú°¡ ³ª¿À°Ô µË´Ï´Ù.

¿¹¸¦ µé¾î)

ÄÚ¿¢½º ½ºÄ«ÀÌ ¶ó¿îÁöÀÇ Á¤º¸°¡ ¾ò°í ½Í´Ù°í Çϸé

MobileOS=ETC

MobileApp=TestName

ServiceKey=¹ß±Þ¹ÞÀº ÀÎÁõÅ°

contentid=1531680

contentTypeid=39

¸¦ ÀÔ·ÂÇØÁÖ½Ã¸é µË´Ï´Ù.

ÀÌ°É À̾ ÁÖ¼Ò·Î Àû¾îº¸¸é

http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?MobileOS=ETC&MobileApp=TestName&ServiceKey=¹ß±Þ¹ÞÀº ÀÎÁõÅ°&contentid=1531680&contentTypeid=39

('¹ß±Þ¹ÞÀº ÀÎÁõÅ°' ºÎºÐ¿¡ ÀÎÁõÅ°¸¦ ³ÖÀ¸¼Å¾ß ÇÕ´Ï´Ù.)

ÀÌ·± Çü½ÄÀÌ µÇ°í URL·Î º¹ºÙÇϸé


ÀÌ·¸°Ô »ý±ä XML ÆÄÀÏÀÌ ³ª¿À°Ô µË´Ï´Ù.

ÀÌ°É ¹Þ¾Æ¿À¸é µÇ´Âµ¥... Å©°Ô µÎ ¹æ½ÄÀÌ ÀÖ¾ú¾î¿ä.

1) JSON ÆĽÌ

2) XML ÆĽÌ

Àú´Â JSONÀ¸·Î ÆĽÌÇÏ´Â ¹ýÀÌ ´õ ÀÌÇØÇϱ⠽±°í Àͼ÷Çؼ­ ÀÌ ¹æ¹ýÀ» È°¿ëÇÏ¿´½À´Ï´Ù.


JAVA ÄÚµå

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void callDetail(HttpServletRequest request, HttpServletResponse response, @RequestParam String contentId,
            @RequestParam String contentTypeId) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
 
        String addr = "http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?ServiceKey=";
        String serviceKey = "ÀÎÁõÅ°";
        String parameter = "";
 
        PrintWriter out = response.getWriter();                                        //Ŭ¶óÀ̾ðÆ®·Î º¸³¾ TEXT DATA, JSON¿¡ ³ÖÀ» ¶§ Âß ³ª¿­Çϴµí
 
        parameter = parameter + "&" + "contentId=" + contentId;        //JSP¿¡¼­ ¹Þ¾Æ¿Ã contentid, contentTypeid
        parameter = parameter + "&" + "contentTypeId=" + contentTypeId;
        parameter = parameter + "&" + "MobileOS=ETC";
        parameter = parameter + "&" + "MobileApp=tour";
        parameter = parameter + "&" + "_type=json";
 
        addr = addr + serviceKey + parameter;
        URL url = new URL(addr);
 
        System.out.println(addr);
 
        InputStream in = url.openStream();                            //URL·Î ºÎÅÍ ÀڹٷΠµ¥ÀÌÅÍ Àоî¿Àµµ·Ï URL°´Ã¼·Î ½ºÆ®¸² ¿­±â
 
        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();        //InputStreamÀÇ µ¥ÀÌÅ͵éÀ» ¹ÙÀÌÆ® ¹è¿­·Î ÀúÀåÇϱâ À§ÇØ
        IOUtils.copy(in, bos1);            //InputStreamÀÇ µ¥ÀÌÅ͸¦ ¹ÙÀÌÆ® ¹è¿­·Î º¹»ç
        in.close();
        bos1.close();
 
        String mbos = bos1.toString("UTF-8");
 
        byte[] b = mbos.getBytes("UTF-8");
        String s = new String(b, "UTF-8");        //StringÀ¸·Î Ç®¾ú´Ù°¡ byte¹è¿­·Î Çß´Ù°¡ ´Ù½Ã StringÀ¸·Î Çؼ­ json¿¡ ÀúÀåÇÒ ¹è¿­À» print?? ¿©±ä Àß ¸ð¸£°Ú´Ù
        out.println(s);
 
        JSONObject json = new JSONObject();
        json.put("data", s);                                                          
    }
cs


JS ÄÚµå

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    daum.maps.event.addListener(marker, 'click'function() {        //´ÙÀ½ ¸Ê¿¡ Àִ ¸¶Ä¿ Å¬¸¯ À̺¥Æ®
 
        $.ajax({        
              url: 'callDetail.do',
              type: 'get',
              data : {"contentId" : contentid, "contentTypeId" : contenttypeid},            //contentid, contentTypeid ¼­¹ö·Î Àü¼Û
              dataType: 'json',
              success: function(data){
                  
                   var myItem = data.response.body.items.item;                                //ÀÌ °æ·Î ³»ºÎ¿¡ µ¥ÀÌÅÍ°¡ µé¾îÀÖÀ½
                   var output = '';
                         output += '<h4>'+myItem.treatmenu+'</h4>';                            //API È°¿ë ¸Þ´º¾ó º¸¸é Response ÇÒ ¼ö ÀÖ´Â
                         output += '<h4>'+myItem.opentimefood+'</h4>';                        //µ¥ÀÌÅ͵éÀÌ ÀÖÀ½
                         output += '<h4>'+myItem.reservationfood+'</h4>';                     //°Å±â¼­ °ñ¶ó ¾²¸é µÊ.
                      
                      $('#window').html(output);
              },
                  error: function(XMLHttpRequest, textStatus, errorThrown) { 
                 alert("Status: " + textStatus); alert("Error: " + errorThrown); 
            } 
    });
    });
    } 
cs


°á°ú¹°


ÀÌ·¸°Ô Áöµµ À§ÀÇ ¸¶Ä¿(Marker)¸¦ Ŭ¸¯Çϸé Ãâ·ÂÇÏ°íÀÚ Çß´ø Ç׸ñµéÀÌ ¶ß°Ô µË´Ï´Ù.