tobby48 пре 4 година
родитељ
комит
a7d0c4afd5

+ 52
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/bus/Bus.java Прегледај датотеку

@@ -0,0 +1,52 @@
1
+package kr.co.swh.lecture.opensource.jackson.bus; 
2
+
3
+import javax.xml.bind.annotation.XmlElement;
4
+
5
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
6
+
7
+/**
8
+ * <pre>
9
+ * kr.co.swh.lecture.opensource.jackson 
10
+ * Bus.java
11
+ *
12
+ * 설명 :
13
+ * </pre>
14
+ * 
15
+ * @since : 2020. 10. 4.
16
+ * @author : tobby48
17
+ * @version : v1.0
18
+ */
19
+@JacksonXmlRootElement(localName = "response")	//	루트 엘리먼트 이름
20
+public class Bus {
21
+	private String comMsgHeader;
22
+	private MessageHeader msgHeader;
23
+	private MessageBody msgBody;
24
+	
25
+	public Bus() {
26
+		// TODO Auto-generated constructor stub
27
+	}
28
+	public String getComMsgHeader() {
29
+		return comMsgHeader;
30
+	}
31
+
32
+	public void setComMsgHeader(String comMsgHeader) {
33
+		this.comMsgHeader = comMsgHeader;
34
+	}
35
+
36
+	public MessageHeader getMsgHeader() {
37
+		return msgHeader;
38
+	}
39
+
40
+	public void setMsgHeader(MessageHeader msgHeader) {
41
+		this.msgHeader = msgHeader;
42
+	}
43
+
44
+	public MessageBody getMsgBody() {
45
+		return msgBody;
46
+	}
47
+
48
+	public void setMsgBody(MessageBody msgBody) {
49
+		this.msgBody = msgBody;
50
+	}
51
+
52
+}

+ 45
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/bus/BusMain.java Прегледај датотеку

@@ -0,0 +1,45 @@
1
+package kr.co.swh.lecture.opensource.jackson.bus;
2
+
3
+import java.io.IOException;
4
+
5
+import org.apache.http.HttpResponse;
6
+import org.apache.http.client.HttpClient;
7
+import org.apache.http.client.methods.HttpGet;
8
+import org.apache.http.impl.client.HttpClients;
9
+
10
+import com.fasterxml.jackson.databind.ObjectMapper;
11
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
12
+
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.jackson
16
+ * JacksonJsonMain.java
17
+ *
18
+ * 설명 :Jackson Json 예제
19
+ * </pre>
20
+ * 
21
+ * @since : 2018. 1. 29.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+public class BusMain {
26
+	private static ObjectMapper mapper = new XmlMapper();
27
+	public static void main(String[] args) throws Exception  {
28
+		HttpClient client = HttpClients.createDefault();
29
+		HttpGet request = new HttpGet("http://openapi.gbis.go.kr/ws/rest/busrouteservice?serviceKey=1234567890&keyword=11");
30
+		try {
31
+			//	필요에 따라서는 헤더 추가
32
+			//		request.addHeader("Content-type", "application/json");
33
+
34
+			//	요청
35
+			HttpResponse response = client.execute(request);
36
+
37
+			//	응답
38
+			Bus value = mapper.readValue(response.getEntity().getContent(), Bus.class);
39
+			System.out.println(value);
40
+		} catch (IOException e2) {
41
+			// TODO Auto-generated catch block
42
+			e2.printStackTrace();
43
+		}
44
+	}
45
+}

+ 38
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/bus/MessageBody.java Прегледај датотеку

@@ -0,0 +1,38 @@
1
+package kr.co.swh.lecture.opensource.jackson.bus; 
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
6
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
7
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8
+
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.opensource.jackson 
13
+ * MessageBody.java
14
+ *
15
+ * 설명 :
16
+ * </pre>
17
+ * 
18
+ * @since : 2020. 10. 4.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+@JacksonXmlRootElement(localName = "busRouteList")	//	루트 엘리먼트 이름
23
+public class MessageBody {
24
+	@JacksonXmlProperty		//	xml의 엘리먼트임을 명시, 엘리먼트 이름을 지정하지 않으면 멤버변수인 'busRouteList'로 지정
25
+    @JacksonXmlElementWrapper(useWrapping = false)
26
+	private List<MessageBodyBus> busRouteList;
27
+	
28
+	public MessageBody() {
29
+		// TODO Auto-generated constructor stub
30
+	}
31
+	public List<MessageBodyBus> getBusRouteList() {
32
+		return busRouteList;
33
+	}
34
+	public void setBusRouteList(List<MessageBodyBus> busRouteList) {
35
+		this.busRouteList = busRouteList;
36
+	}
37
+
38
+}

+ 61
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/bus/MessageBodyBus.java Прегледај датотеку

@@ -0,0 +1,61 @@
1
+package kr.co.swh.lecture.opensource.jackson.bus;
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.jackson 
6
+ * MessageBodyBus.java
7
+ *
8
+ * 설명 :
9
+ * </pre>
10
+ * 
11
+ * @since : 2020. 10. 4.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+public class MessageBodyBus {
16
+
17
+	private String districtCd;
18
+	private String regionName;
19
+	private String routeId;
20
+	private String routeName;
21
+	private String routeTypeCd;
22
+	private String routeTypeName;
23
+	
24
+	public String getDistrictCd() {
25
+		return districtCd;
26
+	}
27
+	public void setDistrictCd(String districtCd) {
28
+		this.districtCd = districtCd;
29
+	}
30
+	public String getRegionName() {
31
+		return regionName;
32
+	}
33
+	public void setRegionName(String regionName) {
34
+		this.regionName = regionName;
35
+	}
36
+	public String getRouteId() {
37
+		return routeId;
38
+	}
39
+	public void setRouteId(String routeId) {
40
+		this.routeId = routeId;
41
+	}
42
+	public String getRouteName() {
43
+		return routeName;
44
+	}
45
+	public void setRouteName(String routeName) {
46
+		this.routeName = routeName;
47
+	}
48
+	public String getRouteTypeCd() {
49
+		return routeTypeCd;
50
+	}
51
+	public void setRouteTypeCd(String routeTypeCd) {
52
+		this.routeTypeCd = routeTypeCd;
53
+	}
54
+	public String getRouteTypeName() {
55
+		return routeTypeName;
56
+	}
57
+	public void setRouteTypeName(String routeTypeName) {
58
+		this.routeTypeName = routeTypeName;
59
+	}
60
+
61
+}

+ 42
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/bus/MessageHeader.java Прегледај датотеку

@@ -0,0 +1,42 @@
1
+package kr.co.swh.lecture.opensource.jackson.bus;
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.jackson 
6
+ * MessageHeader.java
7
+ *
8
+ * 설명 :
9
+ * </pre>
10
+ * 
11
+ * @since : 2020. 10. 4.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+public class MessageHeader {
16
+
17
+	private String queryTime;
18
+	private String resultCode;
19
+	private String resultMessage;
20
+	public MessageHeader() {
21
+		// TODO Auto-generated constructor stub
22
+	}
23
+	public String getQueryTime() {
24
+		return queryTime;
25
+	}
26
+	public void setQueryTime(String queryTime) {
27
+		this.queryTime = queryTime;
28
+	}
29
+	public String getResultCode() {
30
+		return resultCode;
31
+	}
32
+	public void setResultCode(String resultCode) {
33
+		this.resultCode = resultCode;
34
+	}
35
+	public String getResultMessage() {
36
+		return resultMessage;
37
+	}
38
+	public void setResultMessage(String resultMessage) {
39
+		this.resultMessage = resultMessage;
40
+	}
41
+
42
+}

+ 22
- 0
src/test/java/kr/co/swh/lecture/opensource/ApiExplorer3.java Прегледај датотеку

@@ -0,0 +1,22 @@
1
+package kr.co.swh.lecture.opensource; 
2
+
3
+import java.io.IOException;
4
+import java.net.URLEncoder;
5
+
6
+import org.jsoup.Jsoup;
7
+import org.jsoup.nodes.Document;
8
+
9
+public class ApiExplorer3 {
10
+    public static void main(String[] args) throws IOException {
11
+//    	Document doc = Jsoup.connect("http://openapi.gbis.go.kr/ws/rest/busrouteservice?serviceKey=gFPEKeUFchor6ufSmF7rcQdqX07%2FzujLylBDhPciPIFFNGcI6GF4gXbuj%2BA6VYJc0dUETinOu9HQ7ewyXuLqWA%3D%3D&keyword=11").get();
12
+    	
13
+    	String url = "http://openapi.gbis.go.kr/ws/rest/buslocationservice?serviceKey=";
14
+    	String key = URLEncoder.encode("1234567890", "UTF-8");
15
+    	url = url + key + "&routeId=233000031";
16
+    	
17
+    	Document doc = Jsoup.connect(url).get();
18
+    	System.out.println(doc.text());
19
+		
20
+		
21
+    }
22
+}