在一个类方法中看到这一行,我的第一反应是嘲笑编写它的开发人员..但是,我认为我应该确保我是对的.
public void dataViewActivated(DataViewEvent e) {
if (this != null)
// Do some work
}
Run Code Online (Sandbox Code Playgroud)
那条线路会被评估为假吗?
我想使用通用方法来管理5xx错误代码,让我们具体说明db在整个spring应用程序中出现故障的情况.我想要一个漂亮的错误json而不是堆栈跟踪.
对于控制器,我有一个@ControllerAdvice用于不同异常的类,这也是捕获db在请求中间停止的情况.但这并不是全部.我也碰巧有一个自定义CorsFilter扩展OncePerRequestFilter,当我打电话给doFilter我得到CannotGetJdbcConnectionException它,它将不会被管理@ControllerAdvice.我在线阅读了几件让我更加困惑的事情.
所以我有很多问题:
ExceptionTranslationFilter但这只是处理AuthenticationException或AccessDeniedException.HandlerExceptionResolver,但这让我怀疑,我没有任何自定义异常来管理,必须有一个比这更明显的方法.我还尝试添加一个try/catch并调用一个实现HandlerExceptionResolver(应该足够好,我的异常没什么特别的)但是这不会在响应中返回任何内容,我得到一个状态200和一个空体.有什么好方法可以解决这个问题吗?谢谢
我在Zuul有一个场景,URL路由的服务也可能已关闭.因此,响应主体在JSON主体响应中被抛出500 HTTP Status和ZuulException.
{
"timestamp": 1459973637928,
"status": 500,
"error": "Internal Server Error",
"exception": "com.netflix.zuul.exception.ZuulException",
"message": "Forwarding error"
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是自定义或删除JSON响应,并可能更改HTTP状态代码.
我试图用@ControllerAdvice创建一个异常处理程序,但处理程序没有抓住异常.
更新:
所以我扩展了Zuul过滤器,我可以看到它在执行错误后进入run方法,然后如何更改响应.以下是我到目前为止所得到的.我在某处读到了有关SendErrorFilter的内容,但我如何实现它以及它做了什么?
public class CustomFilter extends ZuulFilter {
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
final RequestContext ctx = RequestContext.getCurrentContext();
final HttpServletResponse response = ctx.getResponse();
if (HttpStatus.INTERNAL_SERVER_ERROR.value() == ctx.getResponse().getStatus()) {
try {
response.sendError(404, "Error Error"); //trying to change …Run Code Online (Sandbox Code Playgroud) 我创建了一个过滤器,用于验证 JWT 令牌的每个请求标头:
public class JWTAuthenticationFilter extends GenericFilterBean {
private UserDetailsService customUserDetailsService;
private static Logger logger = LoggerFactory.getLogger(JWTAuthenticationFilter.class);
private final static UrlPathHelper urlPathHelper = new UrlPathHelper();
public JWTAuthenticationFilter(UserDetailsService customUserDetailsService) {
this.customUserDetailsService = customUserDetailsService;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
Authentication authentication = AuthenticationService.getAuthentication((HttpServletRequest) request, customUserDetailsService);
SecurityContextHolder.getContext().setAuthentication(authentication);
if (authentication == null) {
logger.debug("failed authentication while attempting to access " + urlPathHelper.getPathWithinApplication((HttpServletRequest) request));
}
filterChain.doFilter(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
我想抛出一个自定义异常,该异常返回一个响应:
@ResponseStatus(value=HttpStatus.SOMECODE, reason="There was an issue …Run Code Online (Sandbox Code Playgroud)