我正在尝试使用Jersey
开发示例REST API我有以下POJO类
public class TestPOJOObject{
String userName;
int userId;
//Getters and Setters
}
我尝试以两种方式调用我的API
{"userName" : "Shiv" , "userId" : "abcd"}
对于此请求,Jersey会抛出以下错误:
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"])
哪个非常好。
{"userName" : "Shiv" , "userId" : " "}
api成功执行。我监视了后台的值,对于userId,它是'0'。
我无法理解,如何接受空字符串的值并将值赋值为'0'。
我想了解后端实际上发生的这类行为。
我正在使用Jersey 2.x。
答案 0 :(得分:1)
由于您已对userId使用了原始int,因此如果该值为空,则将该值转换为0。如果使用包装器Integer,则会将null作为值。