簡體   English   中英

澤西島上的@QueryParams整數溢出

[英]Overflow @QueryParams Integer in Jersey

澤西島Java

@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

如何處理此錯誤? 我想捕捉錯誤並拋出另一個(我的)錯誤

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 ...
}

這是因為您要傳遞的號碼:

1222534625474

大於Java可以處理的最大整數值:2 ^ 31-1或2147483648。

嘗試將參數類型設置為long:

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

嘗試以下方法。

@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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM