我在这个主题上看到了其他重复的堆栈溢出问题,但是似乎没有人复制我的情况。
当抛出异常时,我的ExceptionHandler类不会捡起它并返回json,而是将带有异常详细信息的默认500代码返回给客户端。我已经检查过了,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);
}
}我在web配置中拥有所有必要的属性:
<!--<context:annotation-config />-->
<tx:annotation-driven/>
<context:component-scan base-package="com.app.controller"/>我的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>研究这个问题已经有一段时间了。
这就是我所得到的:

发布于 2018-06-25 09:13:12
您的异常不是被@ControllerAdvice捕获,因为您是从一个带有@Component注释的类中抛出它的,而不是@Controller。
根据文件:
@Component的专门化,用于声明@ExceptionHandler、@InitBinder或@ModelAttribute方法在多个@Controller类之间共享的类。
您可以找到一个更完整的参考这里。
https://stackoverflow.com/questions/51019544
复制相似问题