【发布时间】:2019-01-17 10:54:30
【问题描述】:
我正在使用 Kotlin 的 Spring Boot 环境中工作。我用@GetMapping 注释的方法制作了一个控制器。此方法有一些 @RequestParam 类型的参数声明为 Double 类型。如果我尝试在不提供这些参数的情况下调用我的方法,我的代码会引发以下异常:
java.lang.IllegalStateException: Optional double parameter 'latitude' is present but cannot be translated into a null value due to being declared as a primitive type.
我假设参数有默认值(可能是 0.0),但是 Kotlin 需要一个可以为 null 的对象,所以会引发异常。
如果我提供参数,一切正常,但如果没有提供参数,我希望我的代码正常工作。
如何避免这个异常?
这是我的控制器的样子:
@RestController
@RequestMapping("/api/stations")
class StationController {
@GetMapping
fun findAll(@RequestParam(value = "latitude", required = false) currentLatitude: Double,
@RequestParam(value = "longitude", required = false) currentLongitude: Double): ResponseEntity<List<Entity>> {
//Method body
}
【问题讨论】:
标签: spring model-view-controller kotlin null