【问题标题】:Handling of Incorrect Value for type Integer as RequestParam in REST API in Spring Boot Application在 Spring Boot 应用程序的 REST API 中处理类型 Integer 作为 RequestParam 的不正确值
【发布时间】:2019-04-08 12:10:00
【问题描述】:

我对测试 API 的其中一种负面情况有疑问。 我有一个带有一个 RestController 的 Spring Boot 应用程序,它接受一个 RequestParam("id") 并且我返回相同的 id 以及 HttpStatus.OK。

当我点击 GET API 时:http://localhost:8080/Temp/getInteger?id=%23abcd 分配给 id 的值是 43981,我不明白它是如何分配的。

理想情况下,我不希望将此字符串转换为整数,有人可以帮我解决这个问题吗?

代码:

@RestController 
@RequestMapping("/Temp")
public class TempController{
  @RequestMapping(method=RequestMethod.GET, path= "/getInteger")
  public ResponseEntity<Object> getInteger(@RequestParam("id") Integer id){
    return new ResponseEntity(id,HttpStatus.OK);
  }
}

请求:

http://localhost:8080/Temp/getInteger?id=%23abcd

输出:

Http Status : 200 OK
Response Body : 43981

【问题讨论】:

    标签: rest spring-boot integer


    【解决方案1】:

    来自 UTF-8 URL 编码字符:%23 是 # 和 从十六进制数系统: abcd 是 43981

    所以它可以转换为 INT。您可以添加一个字母“e”并获得一个新的数字 703710。如果您想避免它,您需要添加验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      相关资源
      最近更新 更多