0

I have a spring web 4.3.4 app with a REST endpoint:

@RequestMapping(value = "/doStuff", method = RequestMethod.GET)
public ResponseEntity<MyDTO[]> findSomething(@RequestParam(value = "status") 
    Optional<Set<EnumStatus>> statusFilter) 
{
     [...];
}

I call it like this: http://localhost:8080/rest/api/doStuff?status=CREATED&status=ACTIVATED

I expected the value of statusFilter to be:

Optional { Set {EnumStatus.CREATED, EnumStatus.ACTIVATED}}

Instead I got:

Optional { Set {EnumStatus.CREATED}}

Turns out Spring is using the ArrayToObjectConverter, reducing the two values to a single one.

Is there a way around this? Is this a bug or expected behaviour?

Michael Böckling
  • 7,341
  • 6
  • 55
  • 76
  • http://stackoverflow.com/questions/2602043/rest-api-best-practice-how-to-accept-list-of-parameter-values-as-input – blank Feb 14 '17 at 09:41
  • Try removing the `Optional` and leave only `Set` and set `required=false` on the `@RequestParam` instead of using an `Optional`. (You also might want to try a newer 4.3.x version as 4.3.6 is the most recent one). Looking at the code for 4.3.4 (and 5.0.0.M4) there might be a small bug in there, looks resolved in newer versions. – M. Deinum Feb 14 '17 at 10:14
  • Yeah thats what I did and it works, but it feels dirty because all of the rest is null-safe with Optional. So "its a bug" is the answer I was looking for in that case, thanks! Post it and I'll mark it as accepted. – Michael Böckling Feb 14 '17 at 10:40

1 Answers1

0
status=CREATED&status=ACTIVATED

How can status be equal to two different values? How about changing your url to

status=["CREATED", "ACTIVATED"]

I'm not clear on how spring would out of the box convert this

blank
  • 17,852
  • 20
  • 105
  • 159