Spring Boot/Kotlin ignore value field of @QueryParam

梦想与她 提交于 2021-01-29 05:44:00

问题


I have a Spring Boot/Kotlin web application and a controller that takes a query param.

Here's my controller method:

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
...
@GetMapping("/items")
@ResponseBody
@Produces(MediaType.APPLICATION_JSON_VALUE)
fun getItems(@QueryParam("n") count: Int? = null): Collection<MyItem>  {
    return myItemService.list(count)
}

The following cURL results in a null count parameter:

curl http://localhost:8080/items?n=25

Instead, Spring Boot always uses the variable name as the query param value:

curl http://localhost:8080/items?count=25

What gives? Is this a bug in Spring Boot, or did the Spring team intentionally ignore the documentation on how @QueryParam should work and choose to favor variable name over annotation value?

来源:https://stackoverflow.com/questions/60725696/spring-boot-kotlin-ignore-value-field-of-queryparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!