Handle HttpMediaTypeNotAcceptableException with 406 status

pull/146/head
johnniang 2019-04-21 10:03:28 +08:00
parent 4cd77ed829
commit 0353c92995
2 changed files with 14 additions and 3 deletions

View File

@ -3,7 +3,6 @@ package run.halo.app.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.template.TemplateExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonComponentModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -44,8 +43,11 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
private static final String FILE_PROTOCOL = "file:///";
@Autowired
private HaloProperties haloProperties;
private final HaloProperties haloProperties;
public WebMvcAutoConfiguration(HaloProperties haloProperties) {
this.haloProperties = haloProperties;
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

View File

@ -1,5 +1,6 @@
package run.halo.app.web.controller.base;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import run.halo.app.exception.HaloException;
import run.halo.app.model.support.BaseResponse;
import run.halo.app.utils.ExceptionUtils;
@ -82,6 +83,14 @@ public class ControllerExceptionHandler {
return baseResponse;
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public BaseResponse handleHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e) {
BaseResponse<?> baseResponse = handleBaseException(e);
baseResponse.setStatus(HttpStatus.NOT_ACCEPTABLE.value());
return baseResponse;
}
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {