Shivkumar Mallesappa
Shivkumar Mallesappa

Reputation: 3077

Jersey takes empty String as a valid Integer value

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

  1. With body {"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.

  1. When I try to call with body {"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

Answers (1)

Abhay Jain
Abhay Jain

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

Related Questions