This commit is contained in:
parent
3c65d00bfb
commit
bdfe68c091
5 changed files with 64 additions and 2 deletions
|
|
@ -0,0 +1,17 @@
|
|||
package ru.spcex.clearing.backendapi.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
@Configuration
|
||||
public class JacksonHttpConverterConfig {
|
||||
@Bean("customJsonHttpConverter")
|
||||
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
|
||||
return new MappingJackson2HttpMessageConverter(mapper);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,39 @@
|
|||
package ru.spcex.clearing.backendapi.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
private final MappingJackson2HttpMessageConverter customJsonHttpConverter;
|
||||
|
||||
@Autowired
|
||||
public WebConfig(@Qualifier("customJsonHttpConverter") MappingJackson2HttpMessageConverter customJsonHttpConverter) {
|
||||
this.customJsonHttpConverter = customJsonHttpConverter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@Override
|
||||
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
|
||||
messageConverters.add(customJsonHttpConverter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs?group=api");
|
||||
|
|
|
|||
|
|
@ -1,19 +1,25 @@
|
|||
package ru.spcex.clearing.backendapi.controller.queue;
|
||||
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
import ru.spcex.clearing.backendapi.controller.response.BasicSpcexResponse;
|
||||
import ru.spcex.clearing.backendapi.errors.ActionValidationException;
|
||||
import ru.spcex.clearing.backendapi.errors.BackEndError;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||
|
|
@ -41,6 +47,23 @@ public class QueueExceptionHandler extends ResponseEntityExceptionHandler {
|
|||
return errorResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
BasicSpcexResponse errorResponse = new BasicSpcexResponse();
|
||||
EnumMessage message;
|
||||
try {
|
||||
UnrecognizedPropertyException jacksonException = (UnrecognizedPropertyException) ex.getCause();
|
||||
message = new EnumMessage(BackEndError.UnknownJsonProperty, jacksonException.getPropertyName());
|
||||
} catch (ClassCastException e) {
|
||||
log.error(ExceptionUtils.getStackTrace(e));
|
||||
message = new EnumMessage(BackEndError.FailedToReadHttpMessage);
|
||||
}
|
||||
errorResponse.setCode(message.getSubject().getId());
|
||||
errorResponse.setMessage(errorResolver.resolve(message));
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(value = Throwable.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ResponseBody
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package ru.spcex.clearing.backendapi.errors;
|
|||
import ru.spcex.platform.utils.enumeration.IEnumId;
|
||||
|
||||
public enum BackEndError implements IEnumId {
|
||||
ValidationError(3000L);
|
||||
ValidationError(3000L),
|
||||
UnknownJsonProperty(3001L),
|
||||
FailedToReadHttpMessage(3002L);
|
||||
private final Long id;
|
||||
|
||||
BackEndError(Long id) {
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
1=This is error example 1.
|
||||
3000=Validation error field: %s
|
||||
3000=validation error field '%s'
|
||||
3001=unrecognized json property '%s'
|
||||
3002=failed to read http message
|
||||
Loading…
Add table
Reference in a new issue