0

I develop a Spring MVC application with a REST API.

I have this method in a controller :

@RequestMapping(value="/roomId/{roomId}",produces = "application/json")
public List<DayStatisticDto> findBySiteAndRoom(@PathVariable("roomId") String roomId, @RequestParam("timestamp") String timestamp, @RequestParam("pageSize") Integer pageSize) throws ParseException {
    List<DayStatisticDto> dayStatisticDtos = dayStatisticService.latestDailyStatisticsForRoom(getCurrentUserSiteCode(), roomId, pageSize, timestamp, false);
    return dayStatisticDtos;
}

When I make this get request :

http://localhost:8080/api/activities/roomId/I1.A.122?timestamp=2016-11-11T12:02:34.421+0100&pageSize=10

With this request parameters :

  • Accept:application/json
  • Accept-Encoding:gzip, deflate, sdch, br
  • Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
  • Cache-Control:no-cache
  • Cookie:_ga=GA1.1.486025197.1475764350;JSESSIONID=6716A5824088BC0F46A6B74D5FB25A3E
  • Host:localhost:8080
  • User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36

I get a correct JSON object as response.

But when I change the parameter "roomId" from I1.A.122 to I1.A.123 :

http://localhost:8080/api/activities/roomId/I1.A.123?timestamp=2016-11-11T12:02:34.421+0100&pageSize=10

With this request parameters :

  • Accept:application/json
  • Accept-Encoding:gzip, deflate, sdch, br
  • Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
  • Cache-Control:no-cache
  • Cookie:_ga=GA1.1.486025197.1475764350; JSESSIONID=6716A5824088BC0F46A6B74D5FB25A3E
  • Host:localhost:8080
  • User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36

I receive a error code 406 - Inacceptable

I use this kind of request everywhere in the projet and I never had this kind of issue. The parameter "roomId" still a normal integer value. How can it generate issue with Spring Mvc ? The headers and the running code still exactly the same.

When I put a breakpoint at the first line of the "findBySiteAndRoom" method, in the first case it reaches the point. But in the second case, it returns the 406 error code before reaching the break point.

Thank you in advance for your help.

Rémi NUYTS
  • 73
  • 1
  • 6
  • When I pass the room ID in requestparam rather than in pathvariable, it's working with the roomId I1.A.123 : http://localhost:8080/api/activities/room?roomId=I1.A.123&timestamp=2016-11-11T12:02:34.421+0100&pageSize=1 When I add an extra word after the roomId, it's working with the room Id I1.A.213 : http://localhost:8080/api/activities/room/I1.A.123/test?timestamp=2016-11-11T12:02:34.421+0100&pageSize=1 How can we explain that ? Is it a bug in Spring MVC ? – Rémi NUYTS Nov 11 '16 at 15:42
  • When I add a "/" just after the roomId value, it's working too : http://localhost:8080/api/activities/roomId/I1.A.123/?timestamp=2016-11-11T12:02:34.421+0100&pageSize=10 – Rémi NUYTS Nov 11 '16 at 15:55
  • your path mapping goes @RequestMapping(value="/roomId/{roomId}",produces = "application/json") or @RequestMapping(value="/room/{roomId}",produces = "application/json") ? (roomId vs room ) – kuhajeyan Nov 11 '16 at 16:39
  • The path mapping is @RequestMapping(value="/roomId/{roomId}",produces = "application/json") – Rémi NUYTS Nov 14 '16 at 07:41

1 Answers1

0

I found a kind of fix. I created a Spring config that extends WebMvcConfigurerAdapter and I overrode the "configureContentNegotiation" method like this :

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}

With this parameter, it works perfectly.

But I still don't understand what can explain that with a room ID I1.A.122 it works and with room ID I1.A.123 it doesn't work.

Rémi NUYTS
  • 73
  • 1
  • 6