Issue
this my is rest request that is compatible for another service :
{
"fromDate": 1562773101000,
"toDate": 1563118701000,
"turnOverType": 4,
"fromAmount": 1,
"toAmount": 10000000,
"voucherDescription": null,
"articleDescription": null,
"referenceNumbers": [],
"offset": 3,
"pageSize": 20,
"iban": "BLAHBLAHBLAHBLAH"
}
and this is corresponding model that not match request :
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TransferRequestInquiryFilter")
public class TransferRequestInquiryFilter implements Serializable {
@XmlElement(name = "sourceIbans")
private List<String> sourceIbans;
@XmlElement(name = "transferType")
private TransferType transferType;
@XmlElement(name = "fromTransferDate")
private Timestamp fromTransferDate;
@XmlElement(name = "toTransferDate")
private Timestamp toTransferDate;
@XmlElement(name = "fromRegistrationDate")
private Timestamp fromRegistrationDate;
@XmlElement(name = "toRegistrationDate")
private Timestamp toRegistrationDate;
@XmlElement(name = "trackingNumbers")
private List<String> trackingNumbers;
@XmlElement(name = "referenceNumbers")
private List<String> referenceNumbers;
@XmlElement(name = "transactionIds")
private List<String> transactionIds;
@XmlElement(name = "status")
private TransactionStatus status;
@XmlElement(name = "fromAmount")
private Long fromAmount;
@XmlElement(name = "toAmount")
private Long toAmount;
@XmlElement(name = "destinationIbans")
private List<String> destinationIbans;
and this is my controller ..
@RequestMapping(value = "/inquiry", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<List<ExtendedTransferRequest>> transferInquiry(@RequestBody @Valid TransferRequestInquiryFilter transferRequestInquiryFilter
, BindingResult bindingResult) {
// when validation not works return bad request
List<ErrorObject> errorObjects = requestInquiryValidator.validate(transferRequestInquiryFilter);
if (errorObjects.size() > 0) {
// just throw bad request and not detail of them
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
List<ExtendedTransferRequest> extendedTransferRequestList = new ArrayList<>();
ExtendedTransferRequest extendedTransferRequest = new ExtendedTransferRequest();
List<SettlementTransaction> settlementTransactionList = settlementSearch.findSettlement(transferRequestInquiryFilter);
extendedTransferRequestList = TransferInquiryResponseMapper.INSTANCE.SettlementTransactionInquiryResponse(setlementTransactionList);
return new ResponseEntity<>(extendedTransferRequestList, HttpStatus.OK);
}
just fromAmount and toAmount fills. but i want to get an exception for this situation and throw a bad request for client. how can i do that? If I get name conflict or type conflict between rest request and model , I need to handle it and riase a bad request for client. i am using spring mvc 5 and jackson-core and jackson-databind 2.9.4
Solution
Using validation-api, annotate proper validation for fields and @validated
before controller method and using @valid
before RequestBody
object would throw proper validation exception.
Answered By - kaveh jozani