brunch

You can make anything
by writing

C.S.Lewis

by 갱그리 May 20. 2016

Spring Boot
에러페이지 Customizing

5/19 개발일기

스프링 부트에서는 에러가 발생하면 기본적으로 Whitelabel Error page로 이동한다. 개발할 때야 뭐 내비두지만, 오픈이 임박하면 이것도 자체 에러페이지로 수정해야 하기 때문에 미리 하기로 했다. 마침 업무에 맞는 exception 들을 만들고 있었기 때문에, 이를 효과적으로 핸들링 하고 싶었다.



1/ 서버 에러 핸들링 Controller 만들기


https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

위 스프링 공식 홈피를 참고했다. 검색해보니 Spring MVC 환경에서 Exception 을 핸들링할 수 있는 공통 컨트롤러를 만들 수 있었다. 일단 /WEB-INF/jsp/ 하위에 error.jsp 를 생성해주고, java/src/project/common/ 하위에 ExceptionHandlingController 를 생성했다. 


error.jsp

<h4>${message}</h4>  <!-- 에러메세지 출력부 -->
<p>
   이용에 불편을 드려 죄송합니다
</p>

jsp에서 ${message} 로 출력하면, Exception Message 가 출력된다 :-)

흔히 exception.getMessage(); 할 때의 그 메시지가 여기 출력된다고 생각하면 된다.


ExceptionHandlingController.java

@RequestMapping("/error")
@Controller
public class ExceptionHandlingController {

    @ExceptionHandler(CustomAuthException.class)
    public ModelAndView LoginAuthError(HttpServletRequest req, CustomAuthException exception) {
      ModelAndView mav = new ModelAndView();
      mav.addObject("exception", exception);
      mav.addObject("url", req.getRequestURL());
      mav.setViewName("error");      
      return mav;      
     }

}


위 예제는 내가 별도로 생성한 CustomAuthException 을 처리하는 부분이다. 로그인이 필요한 페이지에 로그인 없이 들어왔을 때 CustomAuthException 을 내게 되어 있다. 예컨대 이런식으로..


MypageController.java

@RequestMapping(value="/mypage")
    public String mypage(@AuthenticationPrincipal UserDetail userDetail) throws CustomAuthException {        
        if(userDetail == null){
            throw new CustomAuthException("로그인이 필요한 서비스입니다");
        }
....


CustomAuthException 은 이렇다. 별거 없다.


CustomAuthException.java

public class CustomAuthException extends Exception {
    public CustomAuthException(String message) {
        super(message);
    }    
}


이제 Whitelabel Error page 대신 예쁜 자체 에러 페이지를 출력할 소스는 모두 준비되었다. 그러나 이대로 구동해놓고 익센셥을 내면, 아마 또 Whitelabe Error page로 가게 될 것이다.... T.T 여기서 한참 헤맸다. 알고보니 whitelabel.enabled 설정을 먼저 껐어야 했다... 아래와 같이 설정을 추가하자.


application.properties

server.error.whitelabel.enabled=false



2/ 404 페이지 추가하기


사실 예쁘게 코드를 정돈하고 싶었기 때문에, ExceptionHandler에서 어떻게든 404 페이지를 처리하게 하고 싶었으나 알다시피 404는 Server Exception 이 아니라 HTTP STATUS CODE 다. 그래서 그런지 함께 ExceptionHandler에서 구현해 줄 수 있는 방안은 찾지 못했다.(ResourceNotFoundException 등, http code 404 일때 익셉션 내는 방안까지 개발해보기는 했으나 작동이 안됐다)


그래서 위 링크를 따라서, serverproperties 를 추가하는 방식으로 따라했다. 동일하게 java/src/project/common 하위에 ServerCustomization.java 를 추가하여 예제의 소스를 그대로 복+붙하고, Application.java에 해당 bean을 추가했다.


Application.java

@Bean
public ServerProperties getServerProperties() {
    return new ServerCustomization();
}

그리고 /404error.jsp 생성. 여기에서는 ${message} 처럼 변수로 받지 못하고, 그냥 "페이지를 찾을 수 없습니다"라고 박아놨다. 구현하고 매우 슬펐음. 


매거진의 이전글 Spring-Ajax Example (HashMap)
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari