I used @Length and @Size of Hibernate annotations along with String type in java DTO class but String type filed allow all other types like boolean and integer ..etc.
So how to restrict only string type data along with @Length or @Size with message.
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateRoleRequestDto extends BaseDto {
@Size(max = 255, message = "Role description field length should not exceed 255
characters"
private String description;
@Length( max = 50,message = "Role name should not exceed 50 characters")
private String displayName;
}
So here dispalyName and description allow boolean and integer etc . how to restrict to allow only string type along with @Length and @Size with message .
truevalue will result in the"true"string value. Also what makes you think true cannot be a name of the role or 123?true. Looks like you want to restrict something that isn't really an issue. And where do you stop? Strings that look like dates? Strings that look like amounts? Strings that look like Locales, etc. etc. Feels like you are focussing on the wrong thing.trueand"true"as well as888and"888"are the same. If you want to disallow this you are fixing it at the wrong level to start with. I strongly suggest you first start to understand HTTP (and I assume JSON).