Remove default message @Pattern JPA

Asked

Viewed 81 times

1

I need a help from you. I am creating a web service in java using jersey and Ibernate. To validate the fields informed by who calls the service I used for one of the fields the JPA @Pattern annotation, as below:

@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 in 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 shows tbm a default message, which I don’t want to be displayed, as follows:

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

It is possible to delete this message: (path = Cfopcontroller.Cadastrar.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>

1 answer

1

After much headbanging, I finally managed to solve my problem to remove the message. First of all, I noticed that the standard jersey message, as the topic above, appears for any annotation (@Notblank, @Size...) and not only for @Pattern. This occurs, because in the execution of the service, for each violated Constraint (fields of my bean that hold the notes) the jersey throws an exception of type Constraintviolationexception. It turns out, it already has a class set for when this exception is triggered. For this reason, it displays the "unwanted" msg in the service output. So, what did I basically have to do? I had to provide a class that implemented an Exceptionmapper, whose parameter was Constraintviolationexception and write it down with @Provider. This class requires us to implement the toResponse method which returns an object of the Response type. So 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 indicate to the jersey in my web.xml file in the init-param tag that when running it should look for my class in the package indicated. So what he does is he searches my package for annotated classes with @Provider. Thus, he knows that an override must be made of the exception class that fires the Constraintviolationexception. 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 guys. I appreciate anyway the space given to my question and hope that will be useful to other programmers the answer. Hug to all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.