티스토리 뷰

전체 소스는 깃헙에 있습니다.

 

 

@GetMapping("get")
public ResponseEntity<LottoRound> getLottoNum() throws JsonProcessingException {
  LottoRound save = null;
  //20200725 = 921
  String url = "https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=921";

  UriComponents uriComponents = UriComponentsBuilder
  .fromHttpUrl(url)
  .build();
  RestTemplate restTemplate = new RestTemplate();

  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
  httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

  HttpEntity<?> httpEntity = new HttpEntity<>(httpHeaders);

  ResponseEntity<String> exchange = restTemplate.exchange(uriComponents.toUriString(), HttpMethod.GET, httpEntity, String.class);

  String body = exchange.getBody();
  JsonObject obj = JsonParser.parseString(body).getAsJsonObject();
  save = lottoCrawlService.save(LottoRound.createLotto(obj));

  return ResponseEntity.ok(save);
}

 

API를 이용해 받아온 String 값을 Json 파싱 -> Entity에 세팅 후 저장

'개발 > 토이프로젝트' 카테고리의 다른 글

로또사이트 - EC2 인스턴스에 MySql설치  (0) 2020.07.25
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함