티스토리 뷰
구동환경
ㅇ IDE : STS4 (이클립스)
ㅇ API : 한국관광공사 국문관광정보
ㅇ DB : ORACLE
1. application.properties
# datasource
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=사용자
spring.datasource.password=비밀번호
# jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.OracleDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
STS와 ORACLE 연동하는 과정이다.
첫 번째 줄 마지막 :xe 같은 경우 Oracle XE (Express Edition) 을 뜻한다. 시작메뉴에서 "서비스" 를 검색하면 오라클 정보를 알 수 있다.
마지막 줄 선언은 예약어를 사용할 수 있게해준다 ex) CREATE TABLE table;
2. 모델 , 리포지토리 생성
@Data
@Entity
public class AreaBasedList {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
Integer areacode;
String addr1;
String title;
String firstimage;
String tel;
}
public interface AreaBasedListRepository extends JpaRepository<AreaBasedList, Long>{
}
- API를 출력했을 때 나오는 KEY이름과 Model의 변수명, 타입은 통일시켜야한다.
3. DB에 API 정보 저장하기
private final String TourKey = "발급받은 키";
private final String TourUrl = "http://api.visitkorea.or.kr/openapi/service/rest/KorService";
@GetMapping("/getTourApiInfo")
public List<Map<String, Object>> getTourApiInfo() throws Exception {
RestTemplate rt = new RestTemplate();
RequestEntity requestEntity = null;
try {
requestEntity = RequestEntity.get(new URI(
TourUrl + "/areaBasedList"
+ "?serviceKey=" + TourKey
+ "&numOfRows=100"
+ "&pageNo=1"
+ "&MobileOS=ETC"
+ "&MobileApp=AppTest"
+ "&arrange=A"
+ "&contentTypeId=15"
+ "&areaCode=4"
+ "&listYN=Y" + "&_type=json")).build();
} catch (URISyntaxException e) {
e.printStackTrace();
}
ResponseEntity<Map> entity = rt.exchange(requestEntity, Map.class);
Map<String, Object> result = entity.getBody();
Map<String, Object> response = (Map<String, Object>) result.get("response");
Map<String, Object> body = (Map<String, Object>) response.get("body");
Map<String, Object> items = (Map<String, Object>) body.get("items");
List<Map<String, Object>> item = (List<Map<String, Object>>) items.get("item");
AreaBasedList areaBasedList = new AreaBasedList();
for (Map<String, Object> map : item) {
areaBasedList.setAreacode((Integer) map.get("areacode"));
areaBasedList.setAddr1(String.valueOf(map.get("addr1")));
areaBasedList.setTitle(String.valueOf(map.get("title")));
areaBasedList.setFirstimage(String.valueOf(map.get("firstimage")));
areaBasedList.setTel(String.valueOf(map.get("tel")));
/* 저장 */
areaBasedListRepository.save(areaBasedList);
areaBasedList = new AreaBasedList();
}
return item;
}
샘플코드와 큰 차이는 없다. 지역코드가 4번인 지역의 areacode, addr1, title, firstimage, tel 정보를 받아와 DB에 저장해준다.
2번에서 작성한 Model 인스턴스를 생성해 리포지토리의 save명령어로 DB에 반영한다. 반복할 때 마다 새로운 인스턴스를 생성해줘야해서 효율면에서 걱정했으나, 정보 갱신 주기가 길기 때문에 우선은 이렇게 사용했다.
'Programming > Spring' 카테고리의 다른 글
[스프링부트] Thymeleaf 사용법 (0) | 2021.08.08 |
---|---|
[스프링부트] 이클립스 환경설정 (0) | 2021.08.02 |
[스프링부트] Open API (0) | 2021.07.19 |
[스프링부트] 로그 출력 (0) | 2021.07.15 |
[스프링부트] 인터셉터 (0) | 2021.07.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Thymeleaf
- 오라클
- 부트스트랩
- CS
- 환경설정
- 오류
- 넥사크로
- 백준
- Java
- HeidiSQL
- C
- 네트워크
- JSP
- Open API
- C++
- 스프링부트
- 인턴
- 데이터베이스
- 개발용어
- svn
- 프로그래머스
- JVM
- SQL
- 스프링
- CSS
- 이클립스
- 국비교육
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함