You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by charlie <ch...@beebell.com> on 2014/03/03 01:44:37 UTC

Re: jira issue #5401; catching ConstraintViolationException; support for "message" parameter in constraint annotations

I downloaded CXF 3.0 milestone2 and re-built my project against it.

As you said, exceptions that are thrown by validation-interceptors are
now propagated into the ValidationExceptionMapper (configured as a
provider in my application context).

However, when I reconstitute the Response with my custom-entity,
the response-body is empty!  Why?  See code below.

/@javax.ws.rs.ext.Provider
public class CustomExceptionMapper extends ValidationExceptionMapper {

    private final Log log = LogFactory.getLog(this.getClass());

    @Override
    public Response toResponse(javax.validation.ValidationException
exception) {
        if (exception instanceof ConstraintViolationException) {
            Map<String, String> violations = new HashMap<String, String>();
            ConstraintViolationException cev =
(ConstraintViolationException) exception;
            for (ConstraintViolation cv : cev.getConstraintViolations()) {
                log.info("CONSTRAINT VIOLATION: " + cv.getMessage());
                Iterator<Node> nodeIter = cv.getPropertyPath().iterator();
                Node parameter = nodeIter.next();
                violations.put("parameter", parameter.getName());
                violations.put("error", cv.getMessage());
            }
            if (violations.size() > 0) {
                return
Response.status(Status.PRECONDITION_FAILED).entity(violations).type(MediaType.APPLICATION_JSON).build();
            }
        }
        return super.toResponse(exception);
    }
}/


Sergey Beryozkin wrote
> Specifically, in 3.0.0-milestone1, if the exception is initiated from 
> the native CXF interceptor (not JAX-RS filter) then as far as I recall 
> some extra configuration will have to be done to get the JAX-RS 
> exception mappers used.
> In 3.0.0-milestone2 it will just work; alternatively use the bean 
> interceptors registered as JAX-RS 2.0 filters:
> 
> http://cxf.apache.org/docs/validationfeature.html
> 
> HTH
> Sergey
> 
> On 25/02/14 21:58, Sergey Beryozkin wrote:
>> Hi
>> On 25/02/14 17:51, Charlie Read wrote:
>>> In browsing issue #5401,
>>>
>>>      https://issues.apache.org/jira/browse/CXF-5401
>>>
>>> it appears as of Nov, 2014, work was still being done on exceptions
>>> triggered by violations of validation constraints (ie, handling
>>> ConstraintViolationException).
>>>
>>> Here are my questions:
>>>
>>> * Does the current violation-handler propagate up the "message"
>>> encoded in
>>> the
>>> constraint annotation?  IE, does the message reach the consumer of the
>>> API?
>>>
>>> For example, if I have:
>>>
>>>      @Get
>>>      ...
>>>      public Object getSomething()(@QueryParam("color") @NotNull(message
>>> =
>>> "color should be specified") String color) {
>>>      ...
>>>      }
>>>
>>> will the message "color should be specified" be included in the response
>>> body?
>>
>> No, it will be logged only
>>
>>>
>>> * If there is a constraint violation (Eg, in above example, if
>>> color-parameter is null), where is the
>>> ConstraintViolationException handled? Where can it be intercepted and
>>> custom-processed?
>>>
>>> I provisioned a ValidationExceptionMapper (using the CXF 3.0-milestone1
>>> release), but when
>>> I triggered a ConstraintViolationException, the toResponse() method
>>> could
>>> not detect it.
>>>
>>
>> How did you register it ?
>>
>>> * I also noticed in issue #5401 that the logic for returned exceptions
>>> is
>>> in flux.
>>>
>>> Can someone clarify where the logic stands, and specifically what we
>>> should
>>> expect in terms of support for message-propagation back to API consumers
>>> (in the event of ConstraintViolationExceptions)?
>>>
>> I think it is all done as recommended by the JAX-RS 2.0 spec; however
>> please try 3.0.0-milestone2 - there might've been some minor changes
>>
>> Cheers, Sergey
>>
>>> Thanks,
>>> Charlie
>>>
>>
>>
> 
> 
> -- 
> Sergey Beryozkin
> 
> Talend Community Coders
> http://coders.talend.com/
> 
> Blog: http://sberyozkin.blogspot.com





--
View this message in context: http://cxf.547215.n5.nabble.com/Re-jira-issue-5401-catching-ConstraintViolationException-support-for-message-parameter-in-constraints-tp5740482p5740680.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: jira issue #5401; catching ConstraintViolationException; support for "message" parameter in constraint annotations

Posted by Sergey Beryozkin <sb...@gmail.com>.
probably because you put a Map as an entity and I guess the runtime 
fails to find a provider capable of serializing Map as JSON.

You need to register either Jackson (I guess it can deal with Map 
directly) or JSONProvider, if the latter, the ensure the map is wrapped 
in a JAXB bean

HTH, Sergey

On 03/03/14 00:44, charlie wrote:
> I downloaded CXF 3.0 milestone2 and re-built my project against it.
>
> As you said, exceptions that are thrown by validation-interceptors are
> now propagated into the ValidationExceptionMapper (configured as a
> provider in my application context).
>
> However, when I reconstitute the Response with my custom-entity,
> the response-body is empty!  Why?  See code below.
>
> /@javax.ws.rs.ext.Provider
> public class CustomExceptionMapper extends ValidationExceptionMapper {
>
>      private final Log log = LogFactory.getLog(this.getClass());
>
>      @Override
>      public Response toResponse(javax.validation.ValidationException
> exception) {
>          if (exception instanceof ConstraintViolationException) {
>              Map<String, String> violations = new HashMap<String, String>();
>              ConstraintViolationException cev =
> (ConstraintViolationException) exception;
>              for (ConstraintViolation cv : cev.getConstraintViolations()) {
>                  log.info("CONSTRAINT VIOLATION: " + cv.getMessage());
>                  Iterator<Node> nodeIter = cv.getPropertyPath().iterator();
>                  Node parameter = nodeIter.next();
>                  violations.put("parameter", parameter.getName());
>                  violations.put("error", cv.getMessage());
>              }
>              if (violations.size() > 0) {
>                  return
> Response.status(Status.PRECONDITION_FAILED).entity(violations).type(MediaType.APPLICATION_JSON).build();
>              }
>          }
>          return super.toResponse(exception);
>      }
> }/
>
>
> Sergey Beryozkin wrote
>> Specifically, in 3.0.0-milestone1, if the exception is initiated from
>> the native CXF interceptor (not JAX-RS filter) then as far as I recall
>> some extra configuration will have to be done to get the JAX-RS
>> exception mappers used.
>> In 3.0.0-milestone2 it will just work; alternatively use the bean
>> interceptors registered as JAX-RS 2.0 filters:
>>
>> http://cxf.apache.org/docs/validationfeature.html
>>
>> HTH
>> Sergey
>>
>> On 25/02/14 21:58, Sergey Beryozkin wrote:
>>> Hi
>>> On 25/02/14 17:51, Charlie Read wrote:
>>>> In browsing issue #5401,
>>>>
>>>>       https://issues.apache.org/jira/browse/CXF-5401
>>>>
>>>> it appears as of Nov, 2014, work was still being done on exceptions
>>>> triggered by violations of validation constraints (ie, handling
>>>> ConstraintViolationException).
>>>>
>>>> Here are my questions:
>>>>
>>>> * Does the current violation-handler propagate up the "message"
>>>> encoded in
>>>> the
>>>> constraint annotation?  IE, does the message reach the consumer of the
>>>> API?
>>>>
>>>> For example, if I have:
>>>>
>>>>       @Get
>>>>       ...
>>>>       public Object getSomething()(@QueryParam("color") @NotNull(message
>>>> =
>>>> "color should be specified") String color) {
>>>>       ...
>>>>       }
>>>>
>>>> will the message "color should be specified" be included in the response
>>>> body?
>>>
>>> No, it will be logged only
>>>
>>>>
>>>> * If there is a constraint violation (Eg, in above example, if
>>>> color-parameter is null), where is the
>>>> ConstraintViolationException handled? Where can it be intercepted and
>>>> custom-processed?
>>>>
>>>> I provisioned a ValidationExceptionMapper (using the CXF 3.0-milestone1
>>>> release), but when
>>>> I triggered a ConstraintViolationException, the toResponse() method
>>>> could
>>>> not detect it.
>>>>
>>>
>>> How did you register it ?
>>>
>>>> * I also noticed in issue #5401 that the logic for returned exceptions
>>>> is
>>>> in flux.
>>>>
>>>> Can someone clarify where the logic stands, and specifically what we
>>>> should
>>>> expect in terms of support for message-propagation back to API consumers
>>>> (in the event of ConstraintViolationExceptions)?
>>>>
>>> I think it is all done as recommended by the JAX-RS 2.0 spec; however
>>> please try 3.0.0-milestone2 - there might've been some minor changes
>>>
>>> Cheers, Sergey
>>>
>>>> Thanks,
>>>> Charlie
>>>>
>>>
>>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>> Blog: http://sberyozkin.blogspot.com
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Re-jira-issue-5401-catching-ConstraintViolationException-support-for-message-parameter-in-constraints-tp5740482p5740680.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>