rest controller practice : open api 사용하기
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@ package com.example.restcontroller.extra.controller;
|
||||
|
||||
import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.extra.model.OpenApiResult;
|
||||
import com.example.restcontroller.extra.model.PharmacySearch;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -9,11 +10,14 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collections;
|
||||
|
||||
@RestController
|
||||
@@ -43,7 +47,7 @@ public class ApiExtraController {
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
@GetMapping("/api/extra/pharmacy")
|
||||
@GetMapping("/api/extra/pharmacy/json")
|
||||
public ResponseEntity<?> chapter4_5() {
|
||||
|
||||
String apiKey = "JCkqoWYEf9zq0vRVR1TA8Nj6QUIl96TFQZwLnzlr32vbYPx7rJaZvCvq9box6J5WwxpNe5NSKoNzYenwdcXQDA%3D%3D";
|
||||
@@ -75,4 +79,41 @@ public class ApiExtraController {
|
||||
|
||||
return ResponseResult.success(jsonResult);
|
||||
}
|
||||
|
||||
@GetMapping("/api/extra/pharmacy")
|
||||
public ResponseEntity<?> chapter4_6(@RequestBody PharmacySearch pharmacySearch) {
|
||||
|
||||
String apiKey = "JCkqoWYEf9zq0vRVR1TA8Nj6QUIl96TFQZwLnzlr32vbYPx7rJaZvCvq9box6J5WwxpNe5NSKoNzYenwdcXQDA%3D%3D";
|
||||
String url =String.format("http://apis.data.go.kr/B552657/ErmctInsttInfoInqireService/getParmacyListInfoInqire?serviceKey=%s&pageNo=1&numOfRows=10", apiKey);
|
||||
|
||||
String apiResult = "";
|
||||
|
||||
try {
|
||||
url += String.format("&Q0=%s&Q1=%s",
|
||||
URLEncoder.encode(pharmacySearch.getSearchSido(), "utf-8"),
|
||||
URLEncoder.encode(pharmacySearch.getSearchGugun(), "utf-8"));
|
||||
|
||||
URI uri = new URI(url);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
|
||||
String result = restTemplate.getForObject(uri, String.class);
|
||||
|
||||
apiResult = result;
|
||||
} catch (URISyntaxException | UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
OpenApiResult jsonResult = null;
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
jsonResult = objectMapper.readValue(apiResult, OpenApiResult.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return ResponseResult.success(jsonResult);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ public class OpenApiResultResponseBodyItemsItem {
|
||||
private String dutyTime5s;
|
||||
private Integer dutyTime6c;
|
||||
private String dutyTime6s;
|
||||
private Integer dutyTime7c;
|
||||
private String dutyTime7s;
|
||||
private String hpid;
|
||||
private Integer postCdn1;
|
||||
private String postCdn2;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.restcontroller.extra.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PharmacySearch {
|
||||
|
||||
private String sido;
|
||||
private String gugun;
|
||||
|
||||
public String getSearchSido() {
|
||||
return sido != null ? sido: "";
|
||||
}
|
||||
public String getSearchGugun() {
|
||||
return gugun != null ? gugun: "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user