我正在使用Jetty 9.3.3。我实现了我的自定义错误处理程序类,调用了handle方法,但是不知道为什么导致没有throwable对象:
以下是我班级的代码:
public class TestExecuterServerErrorHandler extends ErrorHandler{
private Logger logger=Logger.getLogger(this.getClass().getName());
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException{
logger.systemWriteOutput("handeling error", 3);
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
response.setContentType("text/xml");
if(th!=null)
{
writeResponseError(response.getStatus(), th, writer);
th.printStackTrace();
}
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
}
输出:
handeling error
handeling error
如何打印句柄方法调用的原因?
答案 0 :(得分:1)
使用Jetty和Jersey时遇到了同样的问题。我通过将属性// Rather than setting -myLocationEnabled to YES directly,
// call this method:
- (void)enableMyLocation
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined)
[self requestLocationAuthorization];
else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted)
return; // we weren't allowed to show the user's location so don't enable
else
[self.view setMyLocationEnabled:YES];
}
// Ask the CLLocationManager for location authorization,
// and be sure to retain the manager somewhere on the class
- (void)requestLocationAuthorization
{
_locationAuthorizationManager = [[CLLocationManager alloc] init];
_locationAuthorizationManager.delegate = self;
[_locationAuthorizationManager requestAlwaysAuthorization];
}
// Handle the authorization callback. This is usually
// called on a background thread so go back to main.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status != kCLAuthorizationStatusNotDetermined) {
[self performSelectorOnMainThread:@selector(enableMyLocation) withObject:nil waitUntilDone:[NSThread isMainThread]];
_locationAuthorizationManager.delegate = nil;
_locationAuthorizationManager = nil;
}
}
设置为ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
来解决它,从而阻止Jersey过早地返回响应(在将异常附加到请求对象之前)。