#Validate Request Body and Parameter in Spring Boot

31 messages · Page 1 of 1 (latest)

unique walrus
#

Hi everyone.
I want to know about validation of request body in spring boot.
I've looked at many blogs and github samples, but I can't solve this problem.
please help me.

late beaconBOT
#

This post has been reserved for your question.

Hey @unique walrus! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

summer anvil
#

What exactly is the problem?

unique walrus
#
@PostMapping("/register")
    public ResponseEntity<?> Register(@RequestBody @Valid CreateUserDto createUserDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResponseEntity
                    .badRequest()
                    .body(new MessageResponse(bindingResult.getAllErrors().toString()));
        return authService.createUserFromRegister(createUserDto);
    }
summer anvil
#

looks fine

unique walrus
#

but validation isnot working.

summer anvil
#

What happens?

unique walrus
#

this is dto file.

summer anvil
#

This doesn't tell me what happens

summer anvil
#

if you are using Spring Boot 3, use jakarta.validation imports

#

and remove all javax stuff from your dependencies

unique walrus
#

so javax library is not worked?

summer anvil
#

In a Jakarta EE project, javax things don't work

#

don't mix them

#

Jakarta EE replaced Java EE

unique walrus
#

my project is maven project.

#

no jakarta EE

summer anvil
#

If you have Spring Boot 3, you hava Jakarta EE

unique walrus
#

don't use javax and use jakarta.validation. right?

summer anvil
#

yes

unique walrus
#

but...

summer anvil
#

@NotEmpty is for strings

#

I guess you want @NotNull or similar

unique walrus
#

@summer anvil
thank you very much.

late beaconBOT