2

爪哇,泽西岛

@GET
@Path("/path1")
public String getFunction(
    @QueryParam("param1") Integer intParam1
) {
    ...
}

发送获取请求

  1. http://domain.cc/path1?param1=1222534625474 // 溢出 Int

  2. http://domain.cc/path1?param1=qweqwe

如何处理这个错误?我想捕捉错误并抛出另一个(我的)错误

4

3 回答 3

3

https://java.net/jira/browse/JERSEY-1263

@GET
public String get(@QueryParam("count") int count, @ErrorParam Map<String, String> errors) {
    if (!errors.isEmpty()) {
        throw new WebApplicationException(...whatever response you want to generate...);
    }
    ... do whatever you want to do if parameters are fine ...
}
于 2014-05-20T14:31:10.523 回答
0

试试下面的。

@GET
@Path("/path1")
public String getFunction(
    @QueryParam("param1") String intParam1Str
) {
    Integer intParam1 = null;
    try {
        intParam1 = Integer.parseInt(intParam1Str");
    } catch (Exception e) {
        //do whatever you want
    }
}
于 2014-05-19T14:38:10.773 回答
-1

这是因为您传入的数字:

1222534625474

大于 java 可以处理的最大整数值:2^31-1 或 2147483648。

尝试将参数类型设置为 long:

@GET
@Path("/path1")
public String getFunction(
    @QueryParam("param1") Long intParam1
) {
    ...
}
于 2014-05-19T14:20:58.117 回答