티스토리 뷰

스프링 부트 REST API WEB 프로젝트

깃헙 링크

https://github.com/choiwoonsik/springboot_RestApi_App_Project/tree/main/restApiSpringBootApp

수행 목록

  1. 환경구성 및 helloworld 출력
  2. H2 DB 연동
  3. Swagger API 문서 연동
  4. REST API 설계
  5. RestControllerAdvice를 이용한 통합 예외 처리
  6. Entity - DTO 분리
  7. MessageSource를 이용해 예외 메시지 다국화
  8. JPA Aduting을 이용해 객체 생성시간/수정시간 적용
  9. 스프링 시큐리티 + Jwt를 이용해서 인증 및 권한 체크
  10. 스프링 시큐리티 AuthenticationEntryPoint, AccessDenied로 인증 및 인가 예외처리
  11. Jwt AccessToken + RefreshToken으로 보안성과 사용자 편의성 고도화하기
  12. JUnit Test (단위 테스트)
  13. JUnit Test (통합 테스트)
  14. OAuth 2.0 정리
  15. OAuth 2.0 카카오 로그인 part.1 Authorization code + Token 발급
  16. OAuth 2.0 카카오 로그인 part.2 토큰으로 회원 가입 / 로그인
  17. OAuth 2.0 카카오 로그인 테스트 검증
  18. 환경별 설정을 위해서profile 분리하기 

환경구성

  • h2 + mysql + lombok + gadle + freemaker

디렉토리 구조

HelloWorld 찍어보기

  • response body로 출력
    @GetMapping("/helloWorld/string")
    @ResponseBody
    public String helloWorldString() {
        return "hello";
    }
  • Hello class를 사용해서 출력
    @GetMapping("/helloWorld/json")
    @ResponseBody
    public Hello helloWorldJson() {
        Hello hello = new Hello();
        hello.setMessage("Hello");
        return hello;
    }
---
    @Getter
    @Setter
    public static class Hello {
        private String message;
    }
  • helloWorld.ftl (view)파일을 이용해서 출력
    @GetMapping("/helloWorld/page")
    public String HelloWorldPage() {
        return "helloWorld";
    }

application.yml 작성 - freemkaer를 사용하므로 그에 맞게 작성

spring:
  freemarker:
    template-loader-path: classpath:/templates
    suffix: .ftl
  • 출력결과

3가지 방법을 사용해서 브라우저에 hello, hello world를 출력해보았다.

 

제각기 다른 URL로 Requeset가 들어오면 DispatcherServlet에 의해 HandlerMapping을 거쳐서 Controller로 보내지고 table에서 @Controller로 등록된 메소드들과 URL의 매핑관계를 확인해서 해당 요청에 대해 처리를 하게 된다.

 

각 URL에 맞는 메소드는 적절히 처리를 하는데 @ResponseBody가 달려있는 메소드는 return 하는 문자열 or 객체를 바로 반환하게 된다.

 

그렇지 않다면 반환한 문자열에 대해서 ViewResolver가 prefix에는 classpath를 suffix에는 확장자명(.ftl)을 붙여서 변환을 해준다. 그럼 View에서 해당 위치의 일치하는 viewTemplate 파일을 찾아서 반환하게 된다. 

반응형
Comments
반응형
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday