inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

이야기를 나눠요

173만명의 커뮤니티!! 함께 토론해봐요.

스프링부트에서 Environment 프로퍼티 NullPointerException

아래 코드와 같이 컨트롤러에서 Environment로 yaml의 프로퍼티 값을 꺼내서 사용하고 있었는데 @Slf4j @RestController @RequestMapping("/users") public class RefreshAccessToken { @Autowired UsersService usersService; Environment env; public RefreshAccessToken(Environment env) { this.env = env; } @PostMapping("/refreshAccessToken") private ResponseEntity<?> getRefreshAccessToken(HttpServletRequest request, HttpServletResponse response) { // 요청 헤더에서 refresh token 읽기 String authorizationHeader = request.getHeader("refreshToken"); if (authorizationHeader == null || !authorizationHeader.startsWith(env.getProperty("authorization.token.header.prefix"))) { log.warn("RefreshToken validation error #1 : refreshToken header isn't exists"); throw new CustomApiException("refreshToken header isn't exists"); } ... } ... } env에서 NullPointerException이 발생하더라구요... UsersService와 Enviroment 모두 롬복 생성자 주입해도 안되고, @Value 를 사용해보기도 했는데 역시 프로퍼티 값을 못가져왔습니다. yaml은 application.yml에서 각 서버의 profile을 잡도록 해서 사용 중이고, 모든 서버의 profile에 해당 프로퍼티 키, 밸류가 있습니다. 프로퍼티 키 오타도 없구요. env가 계속 원인을 알 수 없는 null이어서 결국 해당 컨트롤러를 삭제하고 새로 컨트롤러를 만들어봤더니 이번엔 이상없이 env 값이 잘 들어옵니다.😇 아래 새로 만든 컨트롤러 코드엔 롬복 생성자 주입을 사용하고 있는데 혹시나 이게 원인인가 싶어 @Autowired 로 필드 주입이랑 수동 생성자 주입 모두 해봤는데 아무런 문제가 없었습니다. 심지어 NPE가 발생했던 코드와 똑같이 짜봤는데 오류가 안생기더라구요. @Slf4j @RequiredArgsConstructor @RequestMapping("/users") @RestController public class RefreshTokenController { private final Environment env; private final IUsersService usersService; @PostMapping("/refreshAccessToken") public ResponseEntity<?> getAccessTokenByRefreshToken(HttpServletRequest request, HttpServletResponse response) { String secret = env.getProperty("token.secret"); String prefix = env.getProperty("authorization.token.header.prefix"); String expiration = env.getProperty("token.access_expiration_time"); // 토큰 읽어오기 String authorizationHeader = request.getHeader("refreshToken"); if (authorizationHeader == null || !authorizationHeader.startsWith(prefix)) { log.warn("RefreshToken validation error #1 : refreshToken header isn't exists"); throw new CustomApiException("refreshToken header isn't exists"); } ... } ... } 현재로선 Environment 초기화 시점이 꼬여서 null인게 가장 의심이 되긴는데 정확한 오류의 원인을 모르겠어서 질문드립니다.

  • springboot
  • property
  • nullpointerexception
  • yaml
  • environment
Sol Park 댓글 0 좋아요 0 조회수 1068

인기 태그

인프런 TOP Writers

주간 인기글