下面列出了org.springframework.http.HttpStatus#METHOD_NOT_ALLOWED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(final HttpRequestMethodNotSupportedException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final StringBuilder builder = new StringBuilder();
builder.append(ex.getMethod());
builder.append(" method is not supported for this request. Supported methods are ");
ex.getSupportedHttpMethods().forEach(t -> builder.append(t + " "));
final ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getLocalizedMessage(), builder.toString());
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
/**
* 请求方式不支持异常
* 比如:POST方式的API, GET方式请求
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
ResponseMessage handleException(HttpRequestMethodNotSupportedException exception) {
return ResponseMessage
.error(HttpStatus.METHOD_NOT_ALLOWED.value(), "不支持的请求方式")
.result(exception.getSupportedHttpMethods());
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseResult handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) {
log.error(HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase(), e);
return ResponseResult.failure(SystemErrorCode.METHOD_NOT_ALLOWED, e);
}
@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(final HttpRequestMethodNotSupportedException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final StringBuilder builder = new StringBuilder();
builder.append(ex.getMethod());
builder.append(" method is not supported for this request. Supported methods are ");
ex.getSupportedHttpMethods().forEach(t -> builder.append(t + " "));
final ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getLocalizedMessage(), builder.toString());
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
/**
* Handle exceptions that result in a "operation not allowed" status.
*/
@ExceptionHandler(value = MethodNotAllowedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ResponseBody
public ErrorInformation handleOperationNotAllowedException(RuntimeException exception)
{
return getErrorInformation(HttpStatus.METHOD_NOT_ALLOWED, exception);
}
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
LOGGER.error("不支持当前请求方法", e);
Response response = Response.failure(DefaultError.METHOD_NOT_SUPPORTED);
response.setExtMessage(e.getMessage());
return new ResponseEntity<>(response, HttpStatus.METHOD_NOT_ALLOWED);
}
private HttpClientErrorException aMethodNotAllowedException() {
return new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ResponseBody
public ErrorResponse requestMethodNotSupportedHandler(final HttpRequestMethodNotSupportedException exception) throws HttpRequestMethodNotSupportedException {
return handler(exception);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
/**
*
* @param request HttpServletRequest对象
* @return
*/
@RequestMapping(path = "/service",method= RequestMethod.DELETE)
@ApiOperation(value="中心服务delete方式请求", notes="test: 返回 502 表示服务不支持DELETE 请求方式")
public ResponseEntity<String> serviceDelete(HttpServletRequest request) {
return new ResponseEntity<String>("center服务 不支持GET 请求方式", HttpStatus.METHOD_NOT_ALLOWED);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public R handleError(HttpRequestMethodNotSupportedException e) {
log.error("不支持当前请求方法:{}", e.getMessage());
return R.fail(ResultCode.METHOD_NOT_SUPPORTED, e.getMessage());
}
/**
* To ensure no one does login through HTTP GET.
* returns METHOD_NOT_ALLOWED.
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseStatus( HttpStatus.METHOD_NOT_ALLOWED )
public void get() {
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorVM processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorVM(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiError> handleMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getMessage(), null);
return new ResponseEntity<>(apiError, apiError.getStatus());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorDTO processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorDTO(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
/**
* 不支持当前请求方法
*
* @param e 异常信息
* @return
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public BaseMessage handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return pubTools.genNoMsg(StatusCode.METHOD_NOT_ALLOWED, e.getMessage());
}