Reputation: 3077
I am trying to develop a sample REST API using Jersey
I have a following POJO Class
public class TestPOJOObject{
String userName;
int userId;
//Getters and Setters
}
I tried calling my API in two ways
{"userName" : "Shiv" , "userId" : "abcd"}
For this request Jersey throws the following error :
Can not construct instance of java.lang.Integer from String value 'abcd': not a valid Integer value
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@805f848; line: 1, column: 50] (through reference chain: com.sample.pojo.TestPOJOObject["useId"])
Which is perfectly fine.
{"userName" : "Shiv" , "userId" : " "}
The api is executed successfully. I monitored the value at the backed , for userId , it is '0'.
I am not able to understand , how the value for empty String is accepted and valued is assigned to '0'.
I want to understand what is happening in the back-end actually for this type of behaviour.
I am using Jersey 2.x.
Upvotes: 1
Views: 158
Reputation: 86
Since you have used primitive int for userId , library converts the value to 0 if it is empty. If you use the wrapper Integer, you would get null as the value.
Upvotes: 1