@ExceptionHandler не запускается? - PullRequest
0 голосов
/ 25 июня 2018

Я видел другие проблемы переполнения стека в этой теме, но, похоже, ни одна из них не повторяла мою ситуацию.

Когда выдается исключение, мой класс ExceptionHandler не собирает его и не возвращает json, вместо этого код 500 по умолчанию с подробностями об исключении возвращается клиенту в виде HTML. Я проверил, и Spring инициализирует мой класс ExceptionHandler, но по какой-то причине методы не вызываются.

GlobalExceptionHandler.class:

@ControllerAdvice
@RequestMapping(produces = "application/json")
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

public GlobalExceptionHandler(){
    LOG.debug("This gets called in logs...");
}

@ExceptionHandler({CustomException.class})
public @ResponseBody ResponseEntity<Object> handleCustomException(HttpServletRequest request,
                                             CustomException ex) {

    LOG.debug("This does not get called...");
    Map<String, Object> response = new HashMap<>();

    response.put("message", ex.getMessage());
    return new ResponseEntity<>(response, ex.getCode());
}
}

CustomException.class:

public class CustomException extends RuntimeException{

private HttpStatus code;
private String message;

public CustomException(final HttpStatus code, final String message){

    this.code = code;
    this.message = message;
}


/**
 * Gets message.
 *
 * @return Value of message.
 */
public String getMessage() {
    return message;
}

/**
 * Sets new code.
 *
 * @param code
 *         New value of code.
 */
public void setCode(HttpStatus code) {
    this.code = code;
}

/**
 * Sets new message.
 *
 * @param message
 *         New value of message.
 */
public void setMessage(String message) {
    this.message = message;
}

/**
 * Gets code.
 *
 * @return Value of code.
 */
public HttpStatus getCode() {
    return code;
}
}

Здесь запускается обработчик исключений:

@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {

@Autowired
private JwtTokenProvider tokenProvider;

@Autowired
private CustomUserDetailsService customUserDetailsService;

private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationFilter.class);

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain
        filterChain) throws ServletException, IOException {

    logger.debug("Filtering request for JWT header verification");

    String jwt = getJwtFromRequest(request);

    logger.debug("JWT Value: {}", jwt);

    if (StringUtils.hasText(jwt) && tokenProvider.validateToken(jwt)) {
        String username = tokenProvider.getUserIdFromJWT(jwt);

        UserDetails userDetails = customUserDetailsService.loadUserByUsername(username);
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken
                (userDetails, null, userDetails.getAuthorities());
        authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

        SecurityContextHolder.getContext().setAuthentication(authentication);
    } else {

        logger.error("{}", new CustomException(HttpStatus.UNAUTHORIZED, "No Valid JWT Token Provided"));
        throw new CustomException(HttpStatus.UNAUTHORIZED, "No Valid JWT Token Provided");
    }

    filterChain.doFilter(request, response);
}
}

У меня есть все необходимые свойства в веб-конфигурации:

<!--<context:annotation-config />-->
<tx:annotation-driven/>
<context:component-scan base-package="com.app.controller"/>

My Web.xml:

<web-app>

<!-- For web context -->
<servlet>
    <servlet-name>appDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Logging -->
<context-param>
    <param-name>logbackConfigLocation</param-name>
    <param-value>/WEB-INF/classes/logback.xml</param-value>
</context-param>

<filter>
    <filter-name>jwtFilter</filter-name>
    <filter-class>com.app.controller.security.filters.JwtAuthenticationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>jwtFilter</filter-name>
    <servlet-name>appDispatcher</servlet-name>
</filter-mapping>

</web-app>

Обдумывал этот вопрос некоторое время ..

Это все, что я получаю:

enter image description here

1 Ответ

0 голосов
/ 25 июня 2018

Ваше исключение не перехватывается @ControllerAdvice, потому что вы выбрасываете его из класса, помеченного @Component, а не @Controller.

Согласно документации:

Специализация @Component для классов, которые объявляют методы @ExceptionHandler, @InitBinder или @ModelAttribute для совместного использования несколькими классами @Controller.

Более полную ссылку можно найти здесь .

...