1

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?

Nate Vaughan
  • 3,471
  • 4
  • 29
  • 47
  • 2
    you should use `@RequestParam` https://stackoverflow.com/questions/26709560/what-is-the-difference-b-w-requestparam-and-queryparam-anotation – Ryuzaki L Mar 17 '20 at 16:52
  • That's helpful but still leaves me curious why the creators of Spring Boot mis-implemented the JAX-RS specification. – Nate Vaughan Mar 18 '20 at 14:50
  • Spring MVC is the actual name of the Spring web framework and is _not_ a JAX-RS implementation. And Spring Boot is Spring's bootstrapping framework to make launching applications simpler. So what you are using is Spring Boot to launch a Spring MVC application. Just some FYIs. – Paul Samsotha Mar 19 '20 at 01:05
  • @PaulSamsotha sure, but why support JAX-RS annotations at all if Spring is going to do it incorrectly? Honestly feels like a bug in Spring, and I'm curious if there's any justification for it. – Nate Vaughan Mar 19 '20 at 02:39
  • _"but why support JAX-RS annotations at all"_ - It doesn't. Which JAX-RS annotations do you think Spring MVC support? The two frameworks are completely different and have no relation at all. – Paul Samsotha Mar 19 '20 at 02:43
  • On further examination, @PaulSamsotha is right: Spring ignores all JAX-RS annotations. I had thought that Spring was responding to `javax.ws.rs.Produces` and `javax.ws.rs.QueryParam`, but on further examination, I found that in both cases I mistook Spring's defaults for it responding to JAX-RS annotations. Specifically, Spring defaults to `Content-Type: application/json` and maps un-annotated method params by name to query params. Carry on. – Nate Vaughan Mar 19 '20 at 03:00

0 Answers0