问题。杰克逊在春天不会抛出任何例外

时间:2015-02-10 12:46:16

标签: java spring spring-mvc jackson

我有一个代码:

public class BigDecimalDeserializer extends JsonDeserializer<BigDecimal> {
    private static final Pattern PATTERN = Pattern
        .compile("^([1-9]+[0-9]*)((\\.)[0-9]+)?$");
    private static final String MESSAGE = "must be a number in format (99 or 99.99)";
    private static final String EMPTY_STRING = "";

    @Override
    public BigDecimal deserialize(JsonParser parser, DeserializationContext context) 
                  throws IOException, JsonProcessingException 
    {
        BigDecimal result = null;
        JsonNode node = parser.getCodec().readTree(parser);
        String text = node.asText();

        if (text != null && !text.equals(EMPTY_STRING)) {
            Matcher matcher = PATTERN.matcher(text);

            if (matcher.matches()) {
                result = new BigDecimal(text);
            } else {
                throw new ApplicationException(MESSAGE);
            }
        }
        return result;
    }
}

当此方法抛出ApplicationException或其他类型的异常时,没有任何反应。

Where this exception catches? And why there is no stacktraces in the console? 附:我试图在 @ControllerAdvice class (@ExceptionHandler(ApplicationException.class) method) 中处理此异常但没有任何反应。

MessageConverter config:

public void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    converter.setObjectMapper(objectMapper);
    converters.add(converter);
    super.configureMessageConverters(converters);
}

ContollerAdvice:

@ExceptionHandler(ApplicationException.class)
protected ResponseEntity<Object> handleWrongRequest(
        RuntimeException exception, WebRequest request) {
    ApplicationException applicationException = (ApplicationException) exception;

    Error error = new Error();
    error.setField(EMPTY);
    error.setMessage(applicationException.getRootMessage());

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(exception, error, headers,
            HttpStatus.FORBIDDEN, request);
}

Spring处理杰克逊的例外情况?

0 个答案:

没有答案