javax validation not working on spring boot

50 views Asked by At

I know there are already threads on this topic but unfortunately they don't help me, also because it seems that everything is OK at the configuration level.

I added the following dependency to the POM:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

I'm using this version of Spring Boot:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

I have a DTO corresponding to the service request like this:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ObjectRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotBlank(message = "Name field cant'be empty")
    private String name;

    private Map<String, String> data;
}

And this is the offending service that receives the object from before and, as the guide suggests, I used @Valid:

@PostMapping("/add-object")
public ObjectResponse addObject(@Valid @RequestBody ObjectRequest request) {
    LOGGER.debug("BEGIN");
    ObjectResponse object = objectService.addObject(request);
    LOGGER.debug("END, object saved: [{}]", object);
    return object;
}

This is the call I make, below the JSON:

{
   "name": "",
   "data": {
      "year": 2019,
      "price": 99.99
   }
}

Nothing happens! I mean, the call doesn't go through, but nothing logs, so it stops at the beginning. If instead I populate the name field, it works correctly.

I tried various things I found around @Validated annotation above the class or method, changing library versions (but I can't use Spring Boot 3.x), I don't know what to try anymore. Has anyone had a similar problem? Do you have any ideas?

0

There are 0 answers