1

I'm trying to throw a ForbiddenException when a user tries to access a page that needs authentication first. However, it gives me a ClassNotFoundException instead. What I wanted to do is handle the ForbiddenException in an exception handler class and return a proper HTML file(error/403). I know I can just return the view from the controller but I want to do this via Exception handler. Please suggest how could this be achieved?

I added implementation 'javax.ws.rs:javax.ws.rs-api:2.0' in build.gradle

import javax.ws.rs.ForbiddenException;    

@GetMapping("/mypage")
    public ModelAndView displayMypage(ModelAndView modelAndView) {

    if (httpSession.getAttribute("token") == null) {
        throw new ForbiddenException();
    }
    String accessToken = 
    httpSession.getAttribute("token").toString();

    // ユーザー情報取得
    GitHubTemplate gitHubTemplate = new GitHubTemplate(accessToken);
    GitHubUserProfile gitHubUserProfile = gitHubTemplate.userOperations().getUserProfile();
    String userName = gitHubUserProfile.getUsername();

    // ユーザー情報をセットして、mypageを返す
    modelAndView.addObject("userName", userName);
    modelAndView.setViewName("mypage");
    return modelAndView;

}
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • I do not agree with marking this question as a duplicate. Question is not the same. The point here is "Why does `throw new ForbiddenException` trigger a `ClassNotFoundException`. This is specifc to javax.ws.rs and I am also looking to solve this (I suspect it needs some kind of underlying implementation). The linked "duplicate" is just a general JVM classloader question. – Sodved Dec 03 '19 at 00:59

0 Answers0