how to use validation in my controller?

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;
}
@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
}
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
4 Replies
JavaBot
JavaBot3d ago
This post has been reserved for your question.
Hey @Fragmented friends! 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 marked as dormant 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.
ayylmao123xdd
ayylmao123xdd3d ago
you can use spring validation and annotations to each field in your dto like @NonNull but then you need a separate class for the data for request body so paymentdto and paymentrequestbodydto or something and then it will throw a validation error when a field is null or some other case and you can use rest controller advice to capture
JavaBot
JavaBot3d ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?