0

I might need your help to understand why my Java Spring code is not working.

I have a HTTP Get Method, in which I'd like to pass two request parameters. While the first parameter is passed perfectly fine, the second one is always null or empty.

This is what the beginning of the Method looks like.

@RequestMapping(value = "/session/{sessionID}", method = RequestMethod.GET)
public ResponseEntity<List<SensordataDTO>> getSensordataBySessionID(
    @PathVariable("sessionID") long sessionID,
    @RequestParam(required = false, name = "startSequence") Optional<Long> startSequence,
    @RequestParam(required = false, name = "endSequence") Optional<Long> endSequence) {

I tried different types for the RequestParam like long, Long and Optional. I also tried to specify the default value =, name =, value = parameters in the @RequestParam annotation. But no matter what, the second parameter in the url is always null / empty string.

In case I do something wrong with the url itself, I formated it this way:

"/session/1?startSequence=4&?endSequence=6"

I hope you have an idea what I am missing.

baao
  • 71,625
  • 17
  • 143
  • 203
amaridev
  • 31
  • 4
  • 1
    `"/session/1?startSequence=4&?endSequence=6"` should be `"/session/1?startSequence=4&endSequence=6"` closing as typo, or is the question really how to add multiple request params in a query string? – baao Jun 14 '18 at 20:19

2 Answers2

1

Your request

/session/1?startSequence=4&?endSequence=6

As it should be, the first part of a RequestParam starts with a ? and any following requestParams start with an &.

That being said, your request should look like this.

/session/1?startSequence=4&endSequence=6

All that was changed was the removal of your second ?.

Marc Estrada
  • 1,657
  • 10
  • 20
Dean
  • 13
  • 1
0

Change it:

"/session/1?startSequence=4&?endSequence=6"

For it:

"/session/1?startSequence=4&endSequence=6"

The ? is used at only first parameter, after it you can add more parameters using &.

Allan Braga
  • 460
  • 5
  • 19