Siva
Siva

Reputation: 25

@Valid for long data type is not working for mandatory check

I have the below input class and when i trigger the api without 'interactionId' param in the input, I expect validation error message "interactionId cannot be empty" but the validation passes through which i guess could be due to the fact that interactionId has a default value of 0.

Can someone pls. help to enforce this validation on the 'long' parameter when its not given in input?

with @NotEmpty for the customerId param, its working as expected. Using @NotEmpty for the long param "interactionId" is throwing a different error that @notempty cannot be used for long.

public class Input {

@NotEmpty(message = "customerId cannot be empty")
private String customerId;

@Valid
@NotNull(message = "interactionId cannot be empty")
private long interactionId;
 // setters and getters

}

my controller class: @RestController public class Controller {

@PostMapping(value="/detailed-customer-transaction", produces = 
MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Object> detailTransactions(@Valid @RequestBody Input 
params)
{
    return new ResponseEntity<>(Dao.detailTransactions(params), HttpStatus.OK);
}

Above issues is resolved after changing to Long instead of long.

Query #2 I need another help. I have a String input param which takes date-time format in below format. Given its a string parameter, how can i validate for the pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Upvotes: 0

Views: 3051

Answers (1)

clevertension
clevertension

Reputation: 7077

long should be Long, because long is a primary type in java, not an object, so Long is an object that can be checked whether it is null

Upvotes: 3

Related Questions