The validators (@Validated @Valid) do not work with Spring and TomEE
Asked Answered
R

1

0

Validators do not work with Spring and TomEE without Maven or Grade.

I created an elementary project. When I enter incorrect data, the validator simply does nothing (no error, no log, nothing neite).

Nothing valid. It does not validate the parameters of the ret services. It does not validate on the created DTO. I tried in many ways.

I'm going crazy help me, please.

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@NotNull
public class PersonaDTO  {


    @Min(0)
    @Max(270)
    private int eta;

    @Pattern(regexp = "[A-Z]+[a-z][a-z]+")
    private String Cognome;

    @Pattern(regexp = "[A-Z]+[a-z][a-z]+")
    private String Nome;


    @Max(240)
    @Min(80)
    private int altezza;


    public PersonaDTO() {
    }


    public PersonaDTO( String cognome, String nome,int eta, int altezza) {
        this.eta = eta;
        Cognome = cognome;
        Nome = nome;
        this.altezza = altezza;
    }

    public String getCognome() {
        return Cognome;
    }

    public void setCognome(String cognome) {
        Cognome = cognome;
    }

    public String getNome() {
        return Nome;
    }

    public void setNome(String nome) {
        Nome = nome;
    }

    public int getAltezza() {
        return altezza;
    }

    public void setAltezza(int altezza) {
        this.altezza = altezza;
    }

    public int getEta() {
        return eta;
    }

    public void setEta(int eta) {
        this.eta = eta;
    }
}


package it.paolo.spring.rest;

@RestController
@Validated
public class RestSpring {

       @RequestMapping(value = "/crea/{cogome}/{nome}/{eta}/{altezza}",
                        produces = "application/json"
         )
        @Validated  @Valid
        public  PersonaDTO creaPersona(
                @PathVariable("cogome") @Pattern(regexp = "[A-Z]+[a-z][a-z]+") @Valid String strCognome,
                @PathVariable("nome") @Valid @Pattern(regexp = "[A-Z]+[a-z][a-z]+") String strNome,
                @PathVariable("eta") int intEta,
                @PathVariable("altezza") int intAletezza
        ) {

                PersonaDTO persona=new PersonaDTO(strCognome,strNome,intEta,intAletezza);

                return persona;
        }

}
Research answered 1/1, 2019 at 18:0 Comment(0)
S
0

@PathVariable is not meant to be validated in order to send back a readable message to the user. As principle a pathVariable should never be invalid.

If a pathVariable is invalid the reason can be a bug generated a bad url (an href in jsp for example). No @Valid is needed and no message is needed, just fix the code;

"the user" is manipulating the url. Again, no @Valid is needed, no meaningful message to the user should be given.

In both cases just leave an exception bubble up until it is catched by the usual Spring ExceptionHandlers in order to generate a nice error page or a meaningful json response indicating the error. In order to get this result you can do some validation using custom editors

Still You want to validate PathVariable you can use org.springframework.validation.annotation.Validated to valid RequestParam or PathVariable

Init ValidationConfig

@Configuration
public class ValidationConfig {
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
        return processor;
    }
}

Add @Validated to your controller handler class, Like :

@RestController
@Validated
public class RestSpring {
....
}

Add validators to your handler method:

@RequestMapping(value = "/crea/{cogome}/{nome}/{eta}/{altezza}", produces = "application/json")
  public  PersonaDTO creaPersona(
                @PathVariable("cogome") @Pattern(regexp = "[A-Z]+[a-z][a-z]+") @Valid String strCognome,
                @PathVariable("nome") @Valid @Pattern(regexp = "[A-Z]+[a-z][a-z]+") String strNome,
                @PathVariable("eta") int intEta,
                @PathVariable("altezza") int intAletezza) 
                {

                PersonaDTO persona=new PersonaDTO(strCognome,strNome,intEta,intAletezza);

                return persona;
        }
Suitable answered 1/1, 2019 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.