2

Validation does not kick in when posting to RestController. Very simple application setup with springboot-starter-validation added to build.gradle. Code is written in Groovy.

Entitiy to be validated:

@Entity
@Table(name = "event")
class Event extends Base {

    @Column(nullable = false, unique = true)
    @NotBlank
    String name
}

Controller:

@RestController
class ApiController {

    @Autowired
    EventService eventService

    @PostMapping("/api/event")
    Event createEvent(@Valid @RequestBody Event event) {
        eventService.save(event)
    }
}

When posting simple json: {"name": ""}to the endpoint "/api/event" I would expect that Spring would throw MethodArgumentNotValidException but the entity is saved with "" as a value for name. Adding BindingResult as parameter to createEvent yield 0 binding errors.

I must be missing something. But I can't spot what it is. Can you? :)

5
  • 1
    In my trial-and-error frenzy I tried that - even though it should not be necessary... The @Validated annotation is validation groups... It did not do the trick.. :) Commented Jan 11, 2022 at 21:27
  • Yes - as I have written in the top of the post :) Commented Jan 11, 2022 at 21:37
  • @EnableWebMvc ? (which is equivalent to DIY/disables "auto configuration" ..in spring-boot!) Commented Jan 12, 2022 at 2:32
  • I have not explicitely stated @EnableWebMvc as I have springboot-starter-web as a dependecy as well... (And I would rather go with autoconfiguration all the way - or so is my thougt, but....) Commented Jan 12, 2022 at 12:17
  • no, bud, absolutely "cannot reproduce"/works like charm: github.com/xerx593/vaildation-groovy-demo (starter used) Commented Jan 12, 2022 at 15:21

1 Answer 1

1

I tried the same with spring-boot-starter-validation in the Maven environment and it is working fine. On giving {"name": ""} it throws MethodArgumentNotValidException.

{
    "timestamp": "2022-01-12T02:20:21.059+00:00",
    "status": 400,
    "error": "Bad Request",
    "trace": ......,
    "message": "Validation failed for object='event'. Error count: 1",
    "errors": [
        {
            "codes": [
                "NotBlank.event.name",
                "NotBlank.name",
                "NotBlank.java.lang.String",
                "NotBlank"
            ],
            "arguments": [
                {
                    "codes": [
                        "event.name",
                        "name"
                    ],
                    "arguments": null,
                    "defaultMessage": "name",
                    "code": "name"
                }
            ],
            "defaultMessage": "must not be blank",
            "objectName": "event",
            "field": "name",
            "rejectedValue": "",
            "bindingFailure": false,
            "code": "NotBlank"
        }
    ],
    "path": "/api/event"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Exact same source code?
The controller is exactly the same. In entity, you have a parent class, but I declared an id variable in the Event class itself for the primary key and it doesn't have a parent class.
Tried using a non-entity class for the sake of debugging. Added a EventFormObject with a single attribute String name with @NotBlank annotation. Result is the same - no validation kicks in...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.