3

I have @RestController with @RequestMapping("/api/my-resource"). I have @GetMapping for extracting certain entity.

@GetMapping(value = "/{firstId},{secondId}")
public ResponseEntity<MyResourceDTO> findMyResource(
                      @PathVariable Long firstId, @PathVariable String secondId) {

    //here I have firstId == null and secondId == null
}

If I replace , with / everything works fine, but the requirement is not to use another /.

I can confirm, that I can enter this method, but both @PathVariables are mapped to null. Does Spring support this kind of mapping? What have I done wrong?

I would like to achieve something similar to this, but I must use Spring.

Edit:

I have seen the solution with List but I don't want to use it, as the ids are of different type, so it's not a duplicate. I'm using <spring.version>4.3.6.RELEASE</spring.version>

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • @Kayaman it's not a duplicate, because parameters are of a different type, so I won't put them to `List` – xenteros Aug 22 '17 at 12:43
  • But are they non-null if you do? Which version of Spring are you using. – Kayaman Aug 22 '17 at 12:43
  • @Kayaman yes, but I can't use regex validator on the endpoint - there is a difference. In Spring, you can specify what regex should a parameter match to enter that endpoint - If it didn't matter, I'd have used params instead of variables. – xenteros Aug 22 '17 at 12:46
  • Try using `_` as delimiter, it should work – OutOfMind Aug 22 '17 at 12:50
  • 1
    @OutOfMind it worked - if you rewrite it to an answer, I'll be happy to accept it – xenteros Aug 22 '17 at 12:58
  • Wait, first your requirement is to use a comma, and now suddenly underscore is fine? I guess your requirements are pretty flexible. Which version of Spring are you working with? – Kayaman Aug 22 '17 at 12:58
  • @Kayaman ok, my requirements are not to go behind another slash – xenteros Aug 22 '17 at 12:59
  • @xenteros, that's great, posted my answer – OutOfMind Aug 22 '17 at 13:03

2 Answers2

3

With your current Spring version, you can try using _ as the delimiter and it should work fine.

I read this once in a Spring Documentation, but can't find the link right now, will add it as soon as I find it.

OutOfMind
  • 874
  • 16
  • 32
1

Given a controller with this declaration ...

@GetMapping(value = "/this/that/find/{firstId},{secondId}")
public ResponseEntity<String> findMyResource(@PathVariable int firstId, @PathVariable String secondId) {
    return new ResponseEntity<>(""+ firstId + "-" + secondId, HttpStatus.OK);
}

... this test passes:

@Test
public void testWithCommaDelimitedPathVariables() throws Exception {
    MvcResult mvcResult = mockMvc.perform(get("/this/that/find/1,a"))
            .andExpect(status().isOk())
            .andReturn();

    Assert.assertEquals("1-a", mvcResult.getResponse().getContentAsString());
}

So, declaring a "Rest endpoint mapped with @PathVariables separated by comma" is supported by Spring.

Using Spring 4.3.10.RELEASE

glytching
  • 44,936
  • 9
  • 114
  • 120
  • That's interesting. Which version are you using? – xenteros Aug 22 '17 at 12:52
  • 2
    Sorry, was just adding that important information :) – glytching Aug 22 '17 at 12:53
  • That's even more interesting as it doesn't work on my machine :D – xenteros Aug 22 '17 at 12:58
  • Ok, well it's certainly supported behaviour. Maybe it's time (if you haven't already done so) to break this down to a mimimal reproducible project - remove any extraneous code, use vanilla spring etc. If the issue is reproducible in that scenario then start debugging. If the issue is not reproducible in that scenario then the explanation lies somewhere else in your project. Sorry I can't provide a root cause diagnosis but even knowing that what you are trying to do is supported by Spring might help direct your investigation. Or at least it will tell you that you are not wasting your time :) – glytching Aug 22 '17 at 13:04