api 구성 요소를 확인한다. server의 요청을 받는다. 어떤 작업을 요청하고 내려줄지 를 구현한다.
즉, api의 요청에 따라 무엇을 할까? 를 의미한다고 이해하였다.
사용자의 입력이 최초로 받아들여지는 위치 .
@RestController로 시작
@CROSSORIGIN
CORS정책을 우회한다. CORS? 스크립트가 다른 출처(포트,도메인중 하나라도) 다르다면, 리소스 접근을 제한한다.
CROSS ORIGIN(origins="httlp://localost:3000" ) 과 같이, 특정 출처에서의 요청을 허용한다.
@RequestMapping("/")
기본 url을 설정한다. mapping을 어노테이션을 통해 진행한것.
@postmapping
복습) post란, 여러개 자원에 수행되며, data를 update하는 요청이다.
postmapping("/create")와 같이 요청이 들어오면, 해당 요청에 따른 처리하는 메소드와 연관이 있다.
responseentity -> http의 응답을 나타내는 클래스 . 코드 헤더 본문등을 담을 수 있다.
@getmapping
동일하나. get( 데이터를 가져오는 것임을 복습) method에 따른 메소드.
*주의 . 가끔 실수하고 @Getmapping만 2개하고 ("{id}")를 안넣는 경우가 있떠라 해보니.
주의하자 .
TODOBACKEND벽에 부딪혔다 . .. .
postman으로 url을 생성해서 날리면 되는데, todobackend로 작동시, cors문제로 인해 작동이되지 않는다.
어떻게 해결할지 고민중이다.
@Restcontroller의 차이를 직접 확인해보자 .
<Response body가 추가된 어노테이션> -> 메소드든 class든 가능
차이가 무엇이냐면, requestbody 어노테이션이 추가되었기 떄문에
해당 return이 html을 찾기 위한 용도가 아닌, request body에 전송되는 용도로 쓰인다.
index.html을 띄우려면, index를 가지고 있어야 한다.
-> resource,static에 넣어도 매핑되지 않는다. 인텔리제이가 , 또 스프링이 위치를 설정하는 기본값은 .
<main안에 webapp을 만들어주면 , 그곳이 기본 경로이다 >
허나 thymeleaf를 넣어주면, template내의 index를 찾아낼 수 있다. 가장 편하고 추천하는 방식.
/ error를 기본에서 바꾸려면
application에서 whitelabel of , controller도 implemets ErrorController
admin/~~ 인 admin controller에선, 공통을 겹치는
@RequestMapping("/admin")을 controller 위에.
@RestControllerAdvice(annotations = RestController.class)
해당 annotation은, restcontroller로 지정한 controller들을 관리한다. (조언을 준다)
page변경과 동시에 객체 전달. (ModelAndView)
#추가
responseentity<원하는객체>시, 응답 형태를 원하는 객체로 표현한다.
@restcontroller + responseentity의 조합으로 응답 형태를 표현하는데,
@RestController
@RequiredArgsConstructor
@Slf4j
public class WeatherController {
private final WeatherService weatherService ;
@GetMapping("/openapitest")
public ResponseEntity<ResponseVo> OpenapiTest(@RequestParam(value = "date") String date) throws Exception {
WeatherRequestVo weatherRequestVo = new WeatherRequestVo() ;
weatherRequestVo.setApiKey("test") ;
weatherRequestVo.setType("json");
weatherRequestVo.setSdate(date);
LocalTime currenttime = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH") ;
weatherRequestVo.setStdHour(currenttime.format(formatter));
LocalDateTime currentdatetime = LocalDateTime.now() ;
DateTimeFormatter formatterDatetime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
weatherRequestVo.setOrdertime(currentdatetime.format(formatterDatetime));
WeatherResponseVo weatherResponseVo = weatherService.getweatherinfolist(weatherRequestVo) ;
log.info("response" + weatherResponseVo.toString()) ;
ResponseVo responseVo = new ResponseVo() ;
if (weatherResponseVo.getList().isEmpty()) {
throw new Exception();
} else {
responseVo.setCode("ok");
responseVo.setMessage(weatherResponseVo.getList().toString());
}
return ResponseEntity.ok(responseVo) ;
http의 body에 JSON 형태로 RESPONSEVO를 변경해서 전송한다가 되겠다.
'백엔드 > 스프링+boot' 카테고리의 다른 글
롬복 + Dto 상세 (0) | 2023.12.27 |
---|---|
테스트 (0) | 2023.12.25 |
Service (0) | 2023.12.21 |
Repository (0) | 2023.12.21 |
구현 (1) | 2023.12.20 |