Validation #NotBlank does not work in connection with #JsonProperty - java

I am trying to apply a #NotBlank validation to a class field. That field has also the #JsonProperty annotation:
#JsonProperty("ID")
#NotBlank(message = "ID must not be blank")
private String id;
When I pass "" or " " to the property and the incoming JSON field is named "ID" in capital letters then I do not see the expected constraint violation. If it is named "id" in lower case, then the JSON validation works properly.
However, in reality the JSON will come from the requester side in capital letters. So, I cannot simply remove #JsonProperty. Is there a way to solve this problem?

Related

Regex validation with #Pattern annotation in spring data

I need password validation (for example a password must contain at least 4 chars, max 8 and min 1 numeric digit).
I have model (of course with getters and setters):
#Entity
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5534441879591858724L;
#Id
#GeneratedValue
private long id;
#NotBlank
#Email
private String email;
#Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)
private String password;
#NotBlank
#Size(min=2, max=30)
private String name;
I'm catching ConstraintViolationException during saving user info to database and use informations from this exception to inform the user during registration what fields must be corrected because of invalid length etc.
Everything is ok with validation, but not with password. I checked regex expression out of this model class and it works ok, but when i put this regex into annotation parameter ( #Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)) it doesn't work and I have an error:
HHH000346: Error during managed flush [Validation failed for classes
[pl.rpf.kingdom.models.User] during persist time for groups
[javax.validation.groups.Default, ] List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must match "^(?=.*\d).{4,8}$"', propertyPath=password, rootBeanClass=class
pl.rpf.kingdom.models.User,
messageTemplate='{javax.validation.constraints.Pattern.message}'} ]]
Please help me with understanding this error and maybe you have some idea how to solve this problem.
Problem was with password encrypting, I forgot about it.
Regex matches mathod was always false, because it was trying to compare password after encrypting.
I solved problem by putting validation out of model class, before password encrypting.
Other way to resolve problem could be using spring #Valid annotation to validate form before trying save it to database (in my situation it could be problematic from other cases).

Dropwizard: make JSON integer parameter mandatory

I am developing a service using Dropwizard. In a POST request I receive a JSON and I want to throw an exception if the format of the request is not valid, in particular if some fields are missing. By default, as in the documentation, the Hibernate Validator is being used. This is the code:
public class ExtranetRequest {
#NotEmpty(message = "extranet_request_id should not be empty")
public String extranet_request_id;
#NotNull
public int hotel_id;
public List<ShopPattern> shop_patterns;
}
Everything works as expected for the field extranet_request_id (an exception is thrown if the field is not present in the JSON). However the request no error is raised if the field hotel_id is missing. I also tried the annotations #NotEmpty, #NotBlank, #Min(0), but none of them worked.
Did you try to make the hotel_id field an Integer?
An int cannot be null so it will be 0 by default which is OK for #NotNull or #Min(0) (it's checking that the number is higher or equal).
#NotEmpty or #NotBlank should make Hibernate Validator throw an exception though.

How to convert firebase data (snake_case) to Java object (camelCase)

I want to map my firebase data object to my pojo. However, my firebase object property name is snake case,
such as; "user_name".
I want to use camelCase on my pojo,
such as; "userName"
I found beautiful answers like this one, however, I couldn't find any sample about snake_case to camelCase mapping.
My pojo;
#SerializedName("content")
private String content;
#SerializedName("user_name")
private String userName;
I'm using the following line of code for mapping. 'content' matches with no problem(with or without #SerializedName annotation) but userName stays as null.
Story story = storySnapshot.getValue(Story.class);
That is also an issue for obfuscation. Is there an elegant way to match the data to pojo?
The problem was #SerializedName annotation. Firebase has its own annotation, which is #PropertyName.
It is important to be careful about getter name because annotation cares about its name too.
The property must be public too.
There is a perfect answer about that on this link.
Final state of my pojo;
#PropertyName("content")
public String content;
#PropertyName("user_name")
public String userName;

Use of _id for document identifier in elastic search

I have a document with _id as a parameter. I have the below JSON
{
"_id":{
"$oid":"52af48b5d55148fa0c199643"
},
"email":"example.ex#example.com"
}
Corresponding Java class:
#Document(indexName = "Test")
public class Test {
#Id
#ObjectId
private String _id;
private String email;
}
The conversion of json object is successful but there is a problem in saving the data to elastic search.
org.elasticsearch.index.mapper.MapperParsingException: Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters
I have seen some references and came to a conclusion that i cannot use _id.. But am looking for a way to use a JsonProperty but it also does not work as i guess ObjectId uses JsonProperty internally.
Kindly provide suggestions
Just use as following
{
"_id":"52af48b5d55148fa0c199643",
"email":"example.ex#example.com"
}
And remove #ObjectId
Like the error message say, you can't use field with name _id inside the document.
To fix that use "replace all"(or function like that that replace substring in other substring) function, and replace all "_id" occurrence with "id"

Data validation with Hibernate

I currently galley with data validation via Hibernate. Especially with the #Pattern annotation
Wholesale verification is always false, no matter what I do, so I can not save the object.
I try this, among other things:
#NotNull
#Size(max=30)
#SafeHtml
#Pattern(regexp="[a-zA-Z]", messsage="the name can only contain letters")
private String name;
et ceci :
#NotNull
#Size(max=30)
#SafeHtml
#Pattern(regexp="\\D", messsage="the name can only contain letters")
private String name;
In both case, if i write "toto," I have the error message that appears.
Someone an idea?
Have you tried [a-zA-Z]*
The patterns you have look like they only capture one letter. You need a * or a + to suggest multiple letters.

Categories

Resources