Empty response body on request validation Error (Apache CXF, JAX

I am using Apache CXF, JAX-RS and JSR-303 validation. Validation works properly, for invalid request objects I have HTTP 400 response and validation errrors in logs, but my response body is empty. How to have details about validation errors in response body?

I use Spring Boot 1.5.6, Apache CXF 3.1.2.

I setup CXF in java configuration

@Bean
BeanValidationFeature commonValidationFeature() {
    return new BeanValidationFeature();
}

@Bean
ValidationExceptionMapper exceptionMapper() {
    return new ValidationExceptionMapper();
}

@Bean
JAXRSParameterNameProvider parameterNameProvider() {
    return new JAXRSParameterNameProvider();
}

@Bean
BeanValidationProvider validationProvider() {
    return new BeanValidationProvider(parameterNameProvider());
}

@Bean
JAXRSBeanValidationInvoker validationInvoker() {
    final JAXRSBeanValidationInvoker validationInvoker = new JAXRSBeanValidationInvoker();
    validationInvoker.setProvider(validationProvider());
    return validationInvoker;
}

@Bean
JAXRSBeanValidationFeature validationFeature() {
    final JAXRSBeanValidationFeature feature = new JAXRSBeanValidationFeature();
    feature.setProvider(validationProvider());
    return feature;
}

then I use those beans to setup org.apache.cxf.endpoint.Server

JAXRSServerFactoryBean serverFactoryBean = new JAXRSServerFactoryBean();
        serverFactoryBean.setProvider(new JacksonJsonProvider(objectMapper));
        serverFactoryBean.setProvider(exceptionMapper());            
serverFactoryBean.setFeatures(Arrays.asList(commonValidationFeature(), 
swagger2Feature()));
        serverFactoryBean.setBus(bus);
        serverFactoryBean.setInvoker(validationInvoker());
        serverFactoryBean.setServiceBeans(Collections.singletonList(myApiImplementation));
        return serverFactoryBean.create();

How to have details about validation errors in response body? Are there some sensible defaults (like in Spring MVC?) Can I customize error response for validation errors?


Default implementation of org.apache.cxf.jaxrs.validation.ValidationExceptionMapper is not providing details about validation errors in response body, but this can be changed easily. When creating new instance of ValidationExceptionMapper one just need to .setAddMessageToResponse(true);

Then details about validation errors are present in response body.

链接地址: http://www.djcxy.com/p/45628.html

上一篇: Jax RS不会将List转换为JSON,而是会抛出一个500状态消息

下一篇: 请求验证中的空响应主体错误(Apache CXF,JAX