1

I receive as a query parameter a String line like

parameter=123,456,789

What I want to obtain is List<Integer> directly in my controller. Something like this:

@GET
@Path(REST_PATH)
@Produces(MediaType.APPLICATION_JSON)
public Response getSomeStuff(@MagicalThing("parameter") List<Integer> requiredList)

What can be done except for custom provider and an additional type:

https://stackoverflow.com/a/6124014?

Update: custom annotation solves the puzzle http://avianey.blogspot.de/2011/12/exception-mapping-jersey.html

Community
  • 1
  • 1
Sergey
  • 437
  • 5
  • 12
  • Why make the code more complex? Just use request to parameter and create and utility to convert string to List of integers. – LHA May 08 '14 at 14:47
  • @Loc, there is some more background: I have mulptiple endpoints with similar (but not totally equal) parameter semantics. I want to keep rules for parameter parsing in one place, because they are the same for same parameter name. – Sergey May 08 '14 at 15:31
  • By creating utility to parse string, you already re-use code. Right? – LHA May 08 '14 at 15:40
  • Yes, but this solution is showing too much: everyone, who wants to add a service, consuming such parameters, will have to call utility explicitly. I would prefer to make it happen implicitly while accepting the request. Plus: the more non-trivial parameters I have - the more calls to utility classes will be needed, which will result in large amount of boilerplate code in every controller. – Sergey May 08 '14 at 16:04

1 Answers1

0

There is no build in mechanism to do this. You will need to split that string by yourself either in your method or in provider, or in your own object which has a constructor with string parameter, for example:

public Response getSomeStuff(@QueryParam("parameter") MyList requiredList) {
    List<String> list = requiredList.getList();
}

where MyList may be:

 public class MyList {
    List<String>  list;
    public MyList(Srting parameter) {
        list = new ArrayList<String>(parameter.split(","));    
    }

    public List<String> getList() {
        return list;   
    }
}

And then obtain my list in your method.

swist
  • 1,111
  • 8
  • 18
  • does this object have to be a simple DTO or I can put some additiona logic in it? – Sergey May 08 '14 at 15:33
  • Whatever suites your needs. Only the parameter in constructor is required. Alternatively you may use the static methods `valueOf` or `fromString` to produce that object. – swist May 08 '14 at 15:51