0

Here my controller:

@PutMapping("{id}")
public Mono<QdCFPresenter> save(
    Long id,
    @RequestBody @Valid @NotNull QdCFPresenter qdcf
) {
    return this.qdcfService.store(qdcf);
}

I need to validate that id and qdcf.id are equals.

The way I need to accomplish that is using javax.validation.

We have all validation rules all encapsulated using javax.validation validations.

For example:

public class QdCFPresenter {

    private Long id;

    @NotNull
    private Long codi;

}

So, is there any way to get it USING javax.validation validations?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • If you are getting the id through path variable then no need of accepting the same id in request body. Why did you designed your API like that? – pcsutar Oct 13 '22 at 15:16
  • Why? Is that not correct? Which is the correct way? – Jordi Oct 13 '22 at 15:43
  • I think no need of accepting same information at two places (request body and path). If you are getting id in path variable then you should not accept the same id in request body. This will help you to avoid the problem that you are facing. You can also remove id from path variable and accept it through request body. If you still want to design your API same way then you can use AOP (Aspect oriented programming) to perform the validations. – pcsutar Oct 13 '22 at 16:45

1 Answers1

2

I need to validate that id and qdcf.id are equals.

I would disagree with this statement.

I would suggest to separate the concepts and have 3 different classes:

  • an incoming request DTO (QdCFPresenter in your case), it shouldn't contain id as it is a part neither of POST nor PUT request
  • a domain model (with id and code)
  • an outgoing response DTO (also with id and code)

Two last ones look similar but have different responsibilities. The response DTO is your contract with consumers, it could maintain its documentation and so on. The domain model is your internal thing and shouldn't be exposed to be able to evolve if required.

And the incoming request DTO is a blueprint for the creation or modification. You shouldn't require it to have id at all.

Andriy Slobodyanyk
  • 1,965
  • 14
  • 15