Remove default message @Pattern JPA

1

I need a help from you. I'm creating a web service rest in java using jersey and hibernate. To validate the fields reported by the caller I used the JPA @Pattern annotation, as follows:

@XmlRootElement
public class Cfop {

@Pattern(regexp = "[0-9]+", message = "{cfop.idCfop.pattern}")
private String  idCfop;
...

The message {cfop.idCfop.pattern} is defined in my ValidationMessages.properties file, as follows:

cfop.idCfop.pattern    = Id ${validatedValue} inv\u00e1lido.

And on my Controller, I have:

@Path(Constante.CFOP_SERVICE_ENDPOINT)
public class CfopController {

private final CfopRepository repository = new CfopRepository();


@POST
@Consumes("application/json; charset=UTF-8")
@Produces("text/plain; charset=UTF-8")
public Response Cadastrar(@Valid Cfop cfop){

When I call my service via postman, it prints the right message, however it also shows a default message, which I do not want to be displayed, as follows:

Id 123s inválido. (path = CfopController.Cadastrar.arg0.idCfop, invalidValue = 123s)

You can suppress this message: (path = CfopController.Count.arg0.idCfop, invalidValue = 123s)?

Note: In my web.xml I have:

  <init-param>
        <param-ame>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
        <param-value>true</param-value>
  </init-param>
    
asked by anonymous 09.11.2016 / 22:12

1 answer

1

After a lot of head banging, I finally solved my problem to remove the message. First of all, I noticed that the jersey's default message, according to the topic above, appears for any annotation (@NotBlank, @Size ...) and not just @Pattern. This happens because in the execution of the service, for each violated constraint (fields of my bean that have the annotations) the jersey throws an exception of type ConstraintViolationException. It turns out, it already has a class set to when this exception is thrown. For this reason, it displays the "unwanted" msg in the service output. So, what did I basically have to do? I needed to provide a class that implemented an ExceptionMapper, whose parameter was ConstraintViolationException and annotate it with @Provider. This class requires that we implement the toResponse method which returns an object of type Response. Then I did:

import java.util.Iterator;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class ConstraintViolationMapper implements      ExceptionMapper<ConstraintViolationException> {

@Override
public Response toResponse(ConstraintViolationException e) {

    String msg= "";
    Iterator<ConstraintViolation<?>> itr =e.getConstraintViolations().iterator();
    while(itr.hasNext())
    {
        ConstraintViolation<?> c =itr.next();
        msg += c.getMessage()+"\n";
    }

    return Response.status(400).entity(msg).build();
}
}

Then the only thing I needed to do was to point to the jersey in my web.xml file in the init-param tag which when running it should look for my class in the given package. So what it does is look in my package for annotated classes with @Provider. Thus, it knows that an override of the exception class that triggers the ConstraintViolationException must be made. See below:

<init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>coloque aqui o pacote onde estão as suas classes anotadas com @Path;coloque aqui o pacote com suas classes anotadas com @Provider</param-value>
</init-param>

So that's it folks. Thanks anyway for the space given to my question and hopefully useful for other programmers the answer. Hugs to all.

    
14.11.2016 / 12:41