Non riesco proprio a ottenere convalide per funzionare. Ho questo semplice punto finale che fa parte di un programma di avvio Primavera:@Valido che non funziona con JAX-RS utilizzando Spring Boot
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(@Valid UserDTO userDTO, @Context UriInfo uriInfo) {
User user = UserParser.parse(userDTO);
userService.save(user);
final URI uri = uriInfo.getAbsolutePathBuilder().path(String.valueOf(user.getId())).build();
return Response.created(uri).build();
}
Poi, l'UserDTO per convalidare:
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserDTO {
private Long id;
// redundant validations, for testing
@NotNull
@Size(min = 5, max = 80, message = "First name too short")
@NotBlank(message = "First name blank")
@NotEmpty(message = "First name empty")
private String firstName;
@NotNull
@Size(min = 2, max = 80, message = "Last name too short")
private String lastName;
@NotNull
private String email;
}
Ed elabora sempre qualsiasi richiesta, anche con campi vuoti. Ho anche creato un endpoint test per vedere se il problema stava avendo le convalide all'interno del DTO
@Path("/validator/{testInt}")
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String validator(@Valid @Min(value = 30, message = "testInt too small") @PathParam("testInt") Integer testInt) {
return String.valueOf(testInt);
}
Ma questo non funziona neanche, restituisce felicemente qualsiasi int che riceve. Nel caso in cui è importante, il mio punto finale è un @Service
, e qui è alle corrispondenti parti del mio albero esperto di dipendenze:
[INFO] +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.22.1:compile
[INFO] | +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b31:compile
[INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] | +- javax.el:javax.el-api:jar:2.2.4:compile
[INFO] | \- org.glassfish.web:javax.el:jar:2.2.4:compile
ho anche impostare i punti di interruzione all'interno HibernateValidator
, e visto che due dei suoi metodi vengono chiamati, così sembra sta funzionando. Solo non convalidante.
EDIT: La mia configurazione maglia
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(RequestContextFilter.class);
register(LoggingFilter.class);
register(JacksonFeature.class);
packages("com");
}
}
convalida dovrebbe funzionare la casella a meno di scansione META-INF è disabilitato, ma non sembra il caso. Ma se lo è, puoi registrare esplicitamente 'ValidationFeature' con Jersey. Se è già funzionante, e non è solo la risposta che ci si aspetta, è necessario configurare il comportamento del report. Imposta la proprietà 'ServerProperties.BV_SEND_ERROR_IN_RESPONSE' su true in Jersey. A parte questo, non posso riprodurre il comportamento semplicemente semplicemente non funzionante, con quello che hai fornito. –