728x90
반응형
SMALL
Spring in Action (5판)
- 스프링 5의 강력한 기능과 생산성을 활용한 웹 애플리케이션 개발
- Walls, Craig 지음
- 제어펍 출판사
- 2020.05.14
스프링 인 액션 개인 스터디 내용을 정리하자.
영화진흥위원회 일별 박스오피스 데이터 출력 Sample (http://www.kobis.or.kr/kobisopenapi/homepg/apiservice/searchServiceInfo.do?serviceId=searchMovieList)
Dependency (gradle)
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation('org.apache.httpcomponents:httpclient:4.5')
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
}
Controller
package com.example.demo.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
public class SampleController {
@GetMapping("/hello")
public String hello() {
return "Sample Hello";
}
@GetMapping("/getKobisData")
public String getKobisData() {
Map<String, Object> result = new HashMap<String, Object>();
String jsonInString = "";
try {
// RestTemplate 타임아웃 설정, 타임아웃 생략 시 new RestTemplate()으로 진행 가능
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
RestTemplate restTemplate = new RestTemplate(factory);
HttpHeaders headers = new HttpHeaders();
HttpEntity<?> entity = new HttpEntity<>(headers);
String url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json";
UriComponents uri = UriComponentsBuilder.fromHttpUrl(url + "?" + "key=430156241533f1d058c603178cc3ca0e&targetDt=20200901").build();
// Rest Call
ResponseEntity<Map> resultMap = restTemplate.exchange(uri.toString(), HttpMethod.GET, entity, Map.class);
result.put("statusCode", resultMap.getStatusCodeValue());
result.put("header", resultMap.getHeaders());
result.put("body", resultMap.getBody());
// String 파싱
ObjectMapper mapper = new ObjectMapper();
jsonInString = mapper.writeValueAsString(resultMap.getBody());
} catch (HttpClientErrorException | HttpServerErrorException e) {
result.put("statusCode", e.getRawStatusCode());
result.put("body" , e.getStatusText());
System.out.println("dfdfdfdf");
System.out.println(e.toString());
} catch (Exception e) {
result.put("statusCode", "999");
result.put("body" , "excpetion오류");
System.out.println(e.toString());
}
return jsonInString;
}
}
결과
728x90
반응형
LIST
'Study > Spring in Action' 카테고리의 다른 글
[Spring in Action] Stateless vs Stateful (0) | 2021.01.24 |
---|---|
[Spring in Action] Spring RestTemplate (0) | 2021.01.24 |
[Spring in Action] Spring REST (0) | 2021.01.24 |
[Spring in Action] Spring 구성 속성 사용하기 (0) | 2021.01.24 |
[Spring in Action] Spring Security & LDAP & CSRF (0) | 2021.01.24 |