1

Right now I'm working with a project in Spring MVC with RESTful, I have some problems with URL's containing more than one of the same parameter

I would like to request something like:

get/user/{userID}/report/[year/{year}/week/{week},year/{year}/week/{week},....]

and try to parse it into List's with Strings

Is is even possible?

tereško
  • 56,151
  • 24
  • 92
  • 147
We4sZ
  • 55
  • 6
  • I don't know much about using Spring for REST, so I can't help with that specifically, but it almost seems from that structure that it'd be more appropriate to have those repeated parameters as query parameters instead (get/user/{userID}/report?year=x&week=a&year=y&week=b). Have you considered a different approach? – Thor84no Jul 04 '12 at 11:27
  • I might not understand the problem domain here, but it does seem unnecessary to check for a year more than once unless its intervalled? – Henrik Andersson Jul 04 '12 at 11:27
  • I want to be able to get the status for many weeks in different years in the same request – We4sZ Jul 04 '12 at 11:31
  • @Thor84no why is that more appropriate ? And yes you can repeat parameters if you want, but if they are the same whats the point ? – NimChimpsky Jul 04 '12 at 11:35
  • @NimChimpsky I'm not saying it is, it depends a bit on the actual details. In general the main part of the URI points to what you're getting, the query parameters is a selector, so it makes sense semantically. (I'm getting reports, selecting on years/weeks). Also query parameters are already made to support the scenario of multiple values with the same key - it's used for multiple selects in combo-boxes for example. – Thor84no Jul 04 '12 at 11:40
  • @Thor84no it would not be restful then, which this question is tagged as. http://stackoverflow.com/a/671132/106261 – NimChimpsky Jul 04 '12 at 11:44
  • @NimChimpsky I've seen plenty of API's that have query parameters and call themselves REST (libraries such as RESTEasy even provide easy ways of managing them), I think the definition there is wolly. Even if that's the case though, I'd still say he's better served by doing things the best way to achieve the goal, rather than try to stick to arbitrary rules in order to call it REST. – Thor84no Jul 04 '12 at 12:42
  • @Thor84no Doesn't mean those libraries are correct, they are not. Its the accepted definition of rest, its not wooly, its well documented and well used. – NimChimpsky Jul 04 '12 at 12:55
  • @NimChimpsky By that metric what he's asking for - the way I understand it - isn't possible with REST as he's not asking to retrieve a resource, he's asking for multiple resources without providing full identifiers for each resource. get/user/{userID}/report/year/{year}/week/{week} would be fine, but to request multiple you'd need to be less specific (get/user/{userID}/report returning a collection of some kind), or do multiple requests. – Thor84no Jul 04 '12 at 13:03
  • @Thor84no you can retrieve multiple objects just fine with rest, and with one request, and whatever parameters you want – NimChimpsky Jul 04 '12 at 13:05
  • @NimChimpsky I know that, I'm saying as far as I understand that link you posted what he's trying to do wouldn't work. I may have the wrong end of the stick, but the way I understand it you've got a load of reports, which should have URLs .../report/year/x/week/y, but he wants to get the contents of multiple such URLs in one request. I don't see how that fits the narrow definition suggested in your link. More to the point, I don't see how manipulating it to work within that framework would be an improvement over query parameters - URIs aren't made to have repeating patterns like that. – Thor84no Jul 04 '12 at 14:40

1 Answers1

0

You can use like below;

get/user/{userID}/report/[year/{year1}/week/{week1},year/{year2}/week/{week2}

or

use each path param once but seperate data inside with some seperator char like comma and parse it in your java class to obtain it. This way it will be cleaner and unlimited dynamic number of same params can be passed to.

get/user/{userID}/report/[year/{year1}/week/{week1}

where {year} is 2000,2001,2002 and {week} is 2,5,6,7

EDIT 1: Sample code

So something like:

@RequestMapping(value="/test/{tests}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String tests)
{
     String[] test= tests.split(",");
     return "sth"; 
}

You'd pass in:

http://localhost:8080/test/1,3,4,50,xxx,yyy,ddd
fmucar
  • 13,463
  • 2
  • 42
  • 50
  • Noway to do something like this then ? @ RequestMapping(value="/get/user/{userID}/report/[year/{year}/week/{week} ", method = RequestMethod.GET)
    public @ ResponseBody String getWeekView(@ PathVariable String userId, @ PathVariable List year, @ PathVariable List week) {
    }
    – We4sZ Jul 04 '12 at 11:40
  • I dont think there is a direct solution like that. – fmucar Jul 04 '12 at 12:13