我在我的开发中发现了同样的问题,所以要解决这个问题,你需要在你的所有方法中注释的错误消息的响应体@ExceptionHandler
中controllerAdvicer
指定contentType
:
例如:您的接收标头Accept: application/pdf
但您的应用程序生成application/json
.
GlobalExceptionHandler
带有注释的类@ControllerAdvice
以管理特定的正文响应:
package com.obs.sfu.exception;
import javax.validation.ConstraintViolationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.ws.soap.client.SoapFaultClientException;
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
private static final String SIZE_MUST_BE_FROM_1_TO_15_CHARACTERS = "Size must be from 1 to 15 characters";
private static final int CODE_INTERNAL_SERVER_ERROR = 1;
private static final String MESSAGE_INTERNAL_SERVER_ERROR = "Internal error";
private static final String DESCRIPTION_INTERNAL_SERVER_ERROR = "Generic failure message";
private static final int CODE_SERVICE_UNAVAILABLE = 5;
private static final String MESSAGE_SERVICE_UNAVAILABLE = "The service is temporarily unavailable";
private static final String DESCRIPTION_SERVICE_UNAVAILABLE = "The service can not handle the call.";
private static final int CODE_UNSUPPORTED_MEDIA_TYPE = 68;
private static final String MESSAGE_UNSUPPORTED_MEDIA_TYPE = "Unsupported Media Type";
private static final String DESCRIPTION_UNSUPPORTED_MEDIA_TYPE = "The format of the posted body is not supported by the endpoint.";
private static final int CODE_NOT_FOUND = 60;
private static final String MESSAGE_NOT_FOUND = "Resource not found";
private static final String DESCRIPTION_NOT_FOUND = "The Requested URI or the requested resource does not exist.";
private static final int CODE_METHOD_NOT_ALLOWED = 61;
private static final String MESSAGE_METHOD_NOT_ALLOWED = "Method not allowed";
private static final String DESCRIPTION_METHOD_NOT_ALLOWED = "The URI does not support the requested method. The available methods should be set in the response header 'Allow'";
private static final int CODE_NOT_ACCEPTABLE = 62;
private static final String MESSAGE_NOT_ACCEPTABLE = "Not acceptable";
private static final String DESCRIPTION_NOT_ACCEPTABLE = "The Accept incoming header does not match any available content-type.";
private static final int CODE_CONSTRAINT_VIOLATION_FIELD = 20;
private static final String MESSAGE_INVALID_BODY_FIELD = "Invalid Via field";
private static final String HEADER_ALLOW = "Allow";
private static final String HEADER_ALLOW_VALUE = "GET, OPTIONS";
@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_NOT_FOUND, DESCRIPTION_NOT_FOUND,
CODE_NOT_FOUND);
return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(errorDetails);
}
@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_METHOD_NOT_ALLOWED,
DESCRIPTION_METHOD_NOT_ALLOWED, CODE_METHOD_NOT_ALLOWED);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HEADER_ALLOW, HEADER_ALLOW_VALUE);
return ResponseEntity.status(status).headers(responseHeaders).contentType(MediaType.APPLICATION_JSON)
.body(errorDetails);
}
@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_UNSUPPORTED_MEDIA_TYPE,
DESCRIPTION_UNSUPPORTED_MEDIA_TYPE, CODE_UNSUPPORTED_MEDIA_TYPE);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
return ResponseEntity.status(status).headers(responseHeaders).contentType(MediaType.APPLICATION_JSON)
.body(errorDetails);
}
@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_NOT_ACCEPTABLE,
DESCRIPTION_NOT_ACCEPTABLE, CODE_NOT_ACCEPTABLE);
return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(errorDetails);
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_INVALID_BODY_FIELD,
SIZE_MUST_BE_FROM_1_TO_15_CHARACTERS, CODE_CONSTRAINT_VIOLATION_FIELD);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(errorDetails);
}
@ExceptionHandler(SoapFaultClientException.class)
public ResponseEntity<Object> handleSoapFaultClient(Exception ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_SERVICE_UNAVAILABLE,
DESCRIPTION_SERVICE_UNAVAILABLE, CODE_SERVICE_UNAVAILABLE);
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).contentType(MediaType.APPLICATION_JSON)
.body(errorDetails);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGlobalException(Exception ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(request.getContextPath(), MESSAGE_INTERNAL_SERVER_ERROR,
DESCRIPTION_INTERNAL_SERVER_ERROR, CODE_INTERNAL_SERVER_ERROR);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
.body(errorDetails);
}
}
文件application.properties
需要这些属性来管理NoHandlerFoundException
(throwExceptionIfNoHandlerFound
):
spring:
mvc:
throw-exception-if-no-handler-found: true
static-path-pattern: /swagger* # to available swagger-ui.html