#how to use validation service in my controller?

1 messages · Page 1 of 1 (latest)

lethal kraken
#

hey guys. im looking for advice on this question. right now i have this:

 @PostMapping(value = "/myendpoint", produces = "application/json")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<PaymentDTO> createPaymentInformation(@RequestBody @Valid PaymentDTO paymentInformationRequest){
        myValidationService.validateCreatePaymentInformationRequest(paymentInformationRequest);

        PaymentDTO paymentInformationResponse=myService.createPaymentInformation(paymentInformationRequest);
        ResponseEntity<PaymentDTO> responseEntity = new ResponseEntity<>(paymentInformationResponse, HttpStatus.CREATED);
        return responseEntity;
    }

i need to manually check if some fields of my DTO are null. this is my myValidationService's method:

public void validateCreatePaymentInformationRequest(PaymentDTO paymentRequest){
        System.out.println("TransactionID: " + paymentRequest.getTransactionId());
        System.out.println("ClientID: " + paymentRequest.getClientId());
        System.out.println("Amount: " + paymentRequest.getAmount());
        System.out.println("Signature: " + paymentRequest.getSignature());
        //System.exit(1);

        if (paymentRequest.getAmount() == null) {
            //todo return 400 bad req
        }
        if (paymentRequest.getClientId() == null) {
            //todo return 400 bad req
        }

so my questions are:

  1. should i craete try-catch block in my controller, and in try block call the validation method?
  2. or can i leave the controller as is?
  3. if amount or clientId is null, i want to return 400 bad request. do i need to create a custom exception that should be handled in my exception handler?

thanks for your help

novel fernBOT
#

<@&1004656351647117403> please have a look, thanks.

dusky moon
#

Why do you have both that service, and @Valid on your DTO? That's a check one would normally place there.

light plank
#

I may be wrong, but i think you must either manually return back a 400 http response ( i think a java exception will return something like a 500 internal server error) or use the @Valid

reef notch
#

just annotate fields with jakarta's NotNull annotation and create controller advice which overrides handleMethodArgumentNotValid

#

no need to reinevent the wheel

#

it will return 400 and in the overridden method you can add info about what exactly is incorrect

#

this, of course, requires you to have hibernate-validator

lethal kraken
lethal kraken
reef notch
#

For which purpose?

dusky moon
reef notch
#

I have a case where I could derive some of the fields of the request from the auth token, so they can actually be null in some cases, but not others. But if the validation is a simple check that should always be performed, just use annotations.

#

Otherwise, you can throw a ResponseStatusException, or a subtype of it, or any exception annotated with @ResponseStatus