Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- GET방식
- 한글깨짐
- Dispatcher
- WAS
- Break
- 톰캣
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194709&category=questiondetail&tab=community&q=1314387
- CONTINUE
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194690
- xml
- 요구사항정의서
- Request
- Spring MVC
- webserver #WAS #ServerApp
- 매핑 #
- Forwarding
- 피그마
- JSON형식의 response
- 다형성 #부모타입 #자식타입
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194711
- POST방식
- Servlet
- while문
- 김영한
- 화면정의서
- sendRedirect
Archives
- Today
- Total
Step it up now
api사용하여 데이터 받아오기 본문
controller
package com.example.vet.controller.adopt;
import com.example.vet.model.adopt.MissingAnimalResVO;
import com.example.vet.service.adopt.AdoptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//Spring이 HTTP 요청에 대해 JSON 형식의 응답을 생성
@RestController
@Slf4j
@RequestMapping("/api/v1/adopt")
//생성자 주입 가능
@RequiredArgsConstructor
public class AdoptController {
//인스턴스 저장위한 필드선언
private AdoptService adoptService;
@GetMapping("/missed")
//객체 반환하는 메서드/ 누락된 동물에 대한 정보를 반환
public MissingAnimalResVO getMissedAnimal(){
return new MissingAnimalResVO();
}
}
service
package com.example.vet.service.board;
import com.example.vet.config.rest_template.RestTemplateConfig;
import com.example.vet.model.adopt.MissedAnimalReqVO;
import com.example.vet.model.adopt.MissingAnimalResVO;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@Service
//생성자 생성
@RequiredArgsConstructor
public class AdoptService {
//RestTemplateConfig 필드선언 - rest요청 보낼 수 있음
private final RestTemplateConfig restTemplateConfig;
//누락된 동물정보 가져오는 메서드
//MissedAnimalReqVO를 매개변수로 받아와 해당 기간의 누락된 동물 정보를 호출
public MissingAnimalResVO getMissedAnimal(MissedAnimalReqVO reqVO){
String url = "http://apis.data.go.kr/1543061/abandonmentPublicSrvc/abandonmentPublic";
String key = "lEeq8XMbXQj%2B2g%2BUio5fX1q0NYDwSfIAzHJNv3Uf9pABbQsTJZPpNda184SYmT8bxUhvlf4zoyXN%2F6BavDz3jQ%3D%3D";
//API 호출에 필요한 매개변수를 설정하여 MissedAnimalReqVO 객체를 생성
MissedAnimalReqVO missedAnimalReqVO = new MissedAnimalReqVO(key,"20211201","20211231","1","10");
//xml매핑을 위한 xml mapper 객체 생성
XmlMapper xmlMapper = new XmlMapper();
//UriComponentsBuilder를 사용하여 URI를 빌드
//여기서는 쿼리 매개변수를 추가하여 API 호출에 필요한 전체 URI를 생성
UriComponents uriComponents = UriComponentsBuilder
.fromPath(url)
.queryParam("bgnde", missedAnimalReqVO.getBgnde())
.queryParam("endde", missedAnimalReqVO.getEndde())
.queryParam("pageNo",missedAnimalReqVO.getPageNo())
.queryParam("numOfRows",missedAnimalReqVO.getNumOfRows())
.queryParam("serviceKey",missedAnimalReqVO.getServiceKey())
.build();
//RestTemplateConfig를 사용하여 REST 호출.
//URI 문자열을 가져와서 해당 URI에 GET 요청을 보내고, 응답을 문자열로 받음
String str = restTemplateConfig.restTemplate()
.getForObject(uriComponents.toUriString(), String.class);
System.out.println(str);
//MissingAnimalResVO 객체를 반환
//실제로는 API에서 받은 응답을 사용하여 적절한 객체를 생성하여 반환
return new MissingAnimalResVO();
}
}
'프로젝트 > 파이널' 카테고리의 다른 글
정보수정 되지 않음 (0) | 2024.03.13 |
---|---|
<result column="ANIMALPK" property="animalPk"/> (0) | 2024.03.06 |
controller와 jsp 데이터 넘기기 - 오류 발생 (0) | 2024.02.26 |
controller와 jsp 연결 - 오류 발생 (0) | 2024.02.26 |
내 브랜치에서 작업 중에 push 된 부분 pull하기 (0) | 2024.02.22 |