下面列出了org.springframework.http.HttpStatus#is4xxClientError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
// Use the request or status to optionally return a ModelAndView
// System.out.println(status.value());
// System.out.println(status.is5xxServerError());
// System.out.println(model);
if(logger.isErrorEnabled()){
logger.error("View error! ["+status.value()+", "+status.getReasonPhrase()+"]");
}
ModelAndView mav = new ModelAndView();
if(status.is4xxClientError()){
mav.setViewName("error/notFound");
}else{
mav.setViewName("error/serverError");
}
return mav;
}
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
// Use the request or status to optionally return a ModelAndView
// System.out.println(status.value());
// System.out.println(status.is5xxServerError());
// System.out.println(model);
if(logger.isErrorEnabled()){
logger.error("View error! ["+status.value()+", "+status.getReasonPhrase()+"]");
}
ModelAndView mav = new ModelAndView();
if(status.is4xxClientError()){
mav.setViewName("error/notFound");
}else{
mav.setViewName("error/serverError");
}
return mav;
}
@ExceptionHandler(ServiceException.class)
@ResponseBody
public ResponseEntity<RestErrorResponse> serviceException(ServiceException ex) {
ServiceError serviceError = ex.getServiceError();
HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());
RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, ex.getLocalizedMessage());
String logMessage;
try {
logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
} catch (JsonProcessingException e) {
logMessage = "code : " + ex.getServiceError().getCode() + ", message : " + ex.getLocalizedMessage();
}
if (serviceError.equals(ServiceError.ANONYMOUS)) {
// log.info(logMessage);
} else if (httpStatus.is4xxClientError()) {
log.warn(logMessage, ex);
} else if (httpStatus.is5xxServerError()) {
log.error(logMessage, ex);
}
return new ResponseEntity<>(restErrorResponse, HttpStatus.valueOf(serviceError.getHttpStatus()));
}
void logForwardingResult(HttpStatus responseStatus, HttpMethod originalRequestMethod, HttpMethod forwardedRequestMethod, URI originalRequestUri, URI forwardedRequestUri, String mappingName) {
String logMessage = "Forwarding: {} {} -> '{}' -> {} {} {}";
if (responseStatus.is5xxServerError()) {
log(serverErrorLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
} else if (responseStatus.is4xxClientError()) {
log(clientErrorLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
} else {
log(successLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
}
}
void logForwardingResult(HttpStatus responseStatus, String mappingName) {
String logMessage = "Asynchronous execution of '{}' request mapping resulted in {} response status";
if (responseStatus.is5xxServerError()) {
log.error(logMessage, mappingName, responseStatus.value());
} else if (responseStatus.is4xxClientError()) {
log.info(logMessage, mappingName, responseStatus.value());
} else {
log.debug(logMessage, mappingName, responseStatus.value());
}
}
public void send( WebMessage webMessage, HttpServletResponse response, HttpServletRequest request )
{
String type = request.getHeader( "Accept" );
type = !StringUtils.isEmpty( type ) ? type : request.getContentType();
type = !StringUtils.isEmpty( type ) ? type : MediaType.APPLICATION_JSON_VALUE;
HttpStatus httpStatus = HttpStatus.valueOf( webMessage.getHttpStatusCode() );
if ( httpStatus.is4xxClientError() || httpStatus.is5xxServerError() )
{
response.setHeader( "Cache-Control", CacheControl.noCache().cachePrivate().getHeaderValue() );
}
// allow type to be overridden by path extension
if ( request.getPathInfo().endsWith( ".json" ) )
{
type = MediaType.APPLICATION_JSON_VALUE;
}
else if ( request.getPathInfo().endsWith( ".xml" ) )
{
type = MediaType.APPLICATION_XML_VALUE;
}
if ( isCompatibleWith( type, MediaType.APPLICATION_JSON ) )
{
sendJson( webMessage, response );
}
else if ( isCompatibleWith( type, MediaType.APPLICATION_XML ) )
{
sendXml( webMessage, response );
}
else
{
sendJson( webMessage, response ); // default to json
}
}
public static void log(
final Throwable throwable,
final HttpStatus status) {
if (status.is4xxClientError()) {
LOG.warn("{}: {}", status.getReasonPhrase(), throwable.getMessage());
} else if (status.is5xxServerError()) {
LOG.error(status.getReasonPhrase(), throwable);
}
}
@Override
protected boolean hasError(HttpStatus statusCode) {
/**
* When the Eureka server restarts and a client tries to sent a heartbeat the
* server will respond with a 404. By default RestTemplate will throw an
* exception in this case. What we want is to return the 404 to the upstream
* code so it will send another registration request to the server.
*/
if (statusCode.is4xxClientError()) {
return false;
}
return super.hasError(statusCode);
}
@ExceptionHandler({ ServiceException.class })
public ModelAndView handleServiceException(HttpServletRequest request, ServiceException exception) {
ServiceError serviceError = exception.getServiceError();
HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());
RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, exception.getLocalizedMessage());
String logMessage;
try {
logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
} catch (JsonProcessingException e) {
logMessage = "code : " + exception.getServiceError().getCode() + ", message : " + exception.getLocalizedMessage();
}
ModelAndView modelAndView = new ModelAndView();
if (httpStatus.is4xxClientError()) {
modelAndView.setViewName("error/4xx");
log.warn(logMessage, exception);
} else if (httpStatus.is5xxServerError()) {
modelAndView.setViewName("error/" + httpStatus.toString());
log.error(logMessage, exception);
}
Map<String, Object> map = getErrorAttributes(request, false);
modelAndView.addAllObjects(map);
modelAndView.setStatus(httpStatus);
modelAndView.addObject("status", httpStatus.value());
return modelAndView;
}
private void logException(Exception exception, HttpStatus httpStatus) {
if (httpStatus.is4xxClientError()) {
LOG.warn("", exception);
} else {
LOG.error("", exception);
}
}
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
HttpStatus status = clientHttpResponse.getStatusCode();
return status.is4xxClientError() || status.is5xxServerError();
}