ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 경로 에러 추가 하기
    Project/TravelFeelDog 2023. 7. 18. 15:32

    프로젝트 중 경로를 잘못 입력했을때 500 에러가 나는 것을 확인 했다.

     

    api 를 설계하는데 있어 사용자에게 서버 로직에러인 500 에러를 알려주는것은 좋은 설계가 아니라고 한다.

    이를 바꿔 보자.

     

    connection 을 확인하는 간단한 api의 코드이다.

    @RestController
    @RequestMapping("/test")
    @AllArgsConstructor
    public class ConnectionCheckApi {
        @GetMapping(value="/{test-number}")
        public ApiResponse<Long> getConnectTest(@PathVariable Long testNumber){
            return ApiResponse.success(testNumber);
        }
    }
    

     

    위와 같이 포스트맨을 사용하여 경로 에러를 만들어 보면 

     

    경로 에러가 발생했을때 다음과 같은 클라스에러가 확인이 된다.

    org.springframework.web.bind.MissingPathVariableException: Required URI template variable 'testNumber' for method parameter type Long is not presen...

     

     

    RestControllerAdvice 어노테이션이 붙은 클라스에  다음과 같이  추가 해주자.

    @ExceptionHandler(MissingPathVariableException.class)

     

    추가로 404 상태 코드를 출력하기 위하여 @ResponseStatus 도 같이 넣어준다.

     

     

     

     

     

     

    수정 후 결과 상태 코드 404 와 메시지가 잘 보인다

     

     

    관련 깃허브

     

    https://github.com/chosunghyun18/TravelFeelDog-Server

     

    GitHub - chosunghyun18/TravelFeelDog-Server: 여행필독서 앱과 웹을 위한 벡엔드 저장소

    여행필독서 앱과 웹을 위한 벡엔드 저장소 . Contribute to chosunghyun18/TravelFeelDog-Server development by creating an account on GitHub.

    github.com

     

     

    결과도 잘 나오니 공부를 해보자 현재 모르는 부분은 다음과 같다.

     

    1.

    "SpringBoot 에서 경로를 어떻게 인식을 하는 것인가 ?"

     

    2.

    build.graddle 에 다음과 같이 외부 라이브러리를 추가시 404 에러가 아닌 설정하지 않은 401 에러가 나는지?

     

    /**jwt */
    implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
    runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
    runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'
    
    /**spring security*/
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.7.5'

     

     

     

    간단한 해답이 될수 있는 2번 부터 보자.

     

     

    2. 의 대한 해답은  Spring Security 의 관한 자동 설정으로 인하여 생기는 오류이다 .

    프로젝트에 Spring Security 의 의존성만 추가를 해줘도 자동으로 SecurityBuilder 가 포함하고있는 SecurityConfigurer 에 의하여

    보안 관련 빈들이 생성이 됨으로 자동 설정이 이루어진다.

     

    서블렛의 context path 를 추가시 

    servlet:
      context-path: /api/v1

    다음 주소로 로그인 form 창이 나오게 된다.

     

    http://localhost:8080/api/v1/login

     

     

    필자처럼 의존성만 추가를 한 상황에서 아직 auth 처리를 개발 하지 않는 상황이라면 꺼두는 것을 추천한다.

    다음 메인 클라스에있는 어노테이션  옵션으로 보안 관련 자동 설정을 제외 해주자.

    main.class 

    @SpringBootApplication(exclude = SecurityAutoConfiguration.class)

     

     

    남은 질문은 다음 글을 참고 하길 바란다.

     

    Q. "SpringBoot 에서 경로를 어떻게 인식을 하는 것인가 ?"

Designed by Tistory.