Questions tagged [javax.validation]

105 questions
11
votes
6 answers

How to validate type 'java.lang.Boolean' in Spring boot

I have a boolean field which I want to validate to have only "true" or "false" as value(without quotes). But this field is also allowing "true" or "false" as value(with quotes) which I want to restrict. I tried to use @Pattern annotation to match…
Nobita
  • 598
  • 3
  • 6
  • 16
9
votes
1 answer

No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'

I'm using Hibernate validator to validate my beans. Actually I have this dependencies on my POM org.hibernate
9
votes
6 answers

No @NotBlank validator for type String

I keep getting validator error for quite simple configuration class import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import…
Myroslav
  • 91
  • 1
  • 1
  • 2
7
votes
1 answer

How to validate @NotBlank for simple string parameter directly with javax?

I want to validate GET / POST request for spring-boot controller classes with the javax.validation-api annotations. For classes @Valid and @NotBlank for attributes of that class work perfectly. The following works as expected: public class…
Spenhouet
  • 6,556
  • 12
  • 51
  • 76
6
votes
5 answers

@Valid (javax.validation.Valid) is not recursive for the type of list

Controller: @RequestMapping(...) public void foo(@Valid Parent p){ } class Parent { @NotNull // javax.validation.constraints.NotNull private String name; List children; } class Child { @NotNull private String name; } This…
6
votes
4 answers

How to get query parameter name from ConstraintViolationException

I have a service method: @GetMapping(path = "/api/some/path", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getWhatever(@RequestParam(value = "page-number", defaultValue = "0") @Min(0) Integer pageNumber, ... If the caller…
Ihor M.
  • 2,728
  • 3
  • 44
  • 70
5
votes
5 answers

Annotation @NotEmpty doesn't check if String is null

I'm trying to validate some DTOs with javax.validation, but it seems that the annotation @NotEmpty doesn't check of the parameter is null. Here are my clases: Person.class public class Person { @NotEmpty(message = "mandatoryParametersMissing") …
user7274818
5
votes
2 answers

Pass parameter from validate to custom validator in hibernate validator

I'm trying to validate a bean using custom validator. But the validator needs info that is to be passed to it from the method where validate is invoked. Is there a way to do that? I can't pass it in initialize as it is not available at the time of…
Harshini
  • 459
  • 1
  • 7
  • 23
4
votes
1 answer

How to specify order for Javax Validtors in JAX-RS endpoints?

In the following JAX-RS endpoint: public Response myEndpoint(@Context HttpServletRequest httpRequest, @Valid @NotNull …
user3913439
  • 396
  • 1
  • 10
4
votes
4 answers

@Valid not working for spring rest controller

I have defined a rest endpoint method as: @GetMapping("/get") public ResponseEntity getObject(@Valid MyObject myObject){....} This maps request parameters to MyObject. MyObject is defined as(with lombok, javax.validation…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
4
votes
1 answer

javax regex validation of path variables in Spring

I have validation working for the beans and request parameters, however, my path variables fail to get validated: @PathVariable @Pattern(regexp = "[A-Za-z0-9]+") String protocol When I provide path var as ab!ab it doesn't fail the request with 400…
Ihor M.
  • 2,728
  • 3
  • 44
  • 70
4
votes
1 answer

How to i18n a Spring RestController on java.validation constraints?

How can I i18n a spring @RestController property on javax.validation constraints? I thought I could just add /src/main/resources/messages.properties and messages_de.properties, and then spring would detect them and enable proper i18n? But that seems…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
3
votes
1 answer

How to use application.properties values in javax.validation annotations

I have a variable called notification.max-time-to-live in application.yaml file and want to use this as the value of javax.validation.constraints.@Max() annotation. I've tried in many ways (using env.getProperty(), @Value, etc) and it says it must…
3
votes
1 answer

A custom annotation can throw a custom exception, rather than MethodArgumentNotValidException?

I have an @ExceptionHandler(MethodArgumentNotValidException.class) that returns HTTP code 400 when a validation fails. I created a customized annotation and I need to change the HTTP code to 422. But, as the exception handler is already annotated…
romeucr
  • 169
  • 1
  • 13
3
votes
2 answers

Is there an annotation for java validate if the value of a field in a List of Objects is duplicated?

I have a List of Objects and each Object have an email, I'd like to validate if the email is duplicated in this list. @PostMapping @ResponseStatus(HttpStatus.CREATED) Father create(@PathVariable String id, @Valid @RequestBody Father father)…
Guilherme Bernardi
  • 490
  • 1
  • 6
  • 18
1
2 3 4 5 6 7