Jersey将空String作为有效的Integer值

时间:2017-07-21 07:07:59

标签: java jersey-2.0

我正在尝试使用Jersey

开发示例REST API

我有以下POJO类

public class TestPOJOObject{

 String userName;
 int userId;

//Getters and Setters

}

我尝试以两种方式调用我的API

  1. 使用正文{"userName" : "Shiv" , "userId" : "abcd"}
  2. 对于此请求,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"])
    

    哪个非常好。

    1. 当我尝试使用正文{"userName" : "Shiv" , "userId" : " "}
    2. 进行通话时

      api成功执行。我监视了后台的值,对于userId,它是'0'。

      我无法理解,如何接受空字符串的值并将值赋值为'0'。

      我想了解后端实际上发生的这类行为。

      我正在使用Jersey 2.x。

1 个答案:

答案 0 :(得分:1)

由于您已对userId使用了原始int,因此如果该值为空,则将该值转换为0。如果使用包装器Integer,则会将null作为值。