2

I have Rest WS with pathparam like

@Path("/myService/{types}")
public Response myFunction(@Context HttpServletRequest request
                           ,@PathParam("types")String[] types)
{
  Criteria criteria = session.createCriteria(myObject.class);
  criteria.add(Restrictions.in("tranType", types));
  List<myObject> lst= (List<myObject>) criteria.list()
}

or there may have ArrayList or List with the place String[]

I am trying to call this WS from browser.when i am calling it with 1 parameter in array it is working fine. WS call is like /myService/type1. but when i want to call it with 2 or more parameters in array it is not working. response is empty array but it must return fill array . I am calling WS with many parameters like /myService/type1,type2 and it is not working , i tried /myService/{type1,type2} too and /myService/[type1,type2] and /myService/["type1","type2"] and /myService/"type1","type2" but unfortunately nothing works . Can you tell me how can i do it ?

  • you can check out these links http://stackoverflow.com/questions/11944410/passing-array-in-get-for-a-rest-call http://stackoverflow.com/questions/2602043/rest-api-best-practice-how-to-accept-list-of-parameter-values-as-input – Rahul Singh May 03 '17 at 08:15
  • @RahulSingh you might want to insert some whitespace in between the links. – Thomas May 03 '17 at 08:17

3 Answers3

2

As i mentioned in the comment easiest way to accept the path param as String And then split it based on your delimiter like , in the code.

Below is the modified code of yours which shows above mechanism :-

    @Path("/myService/{types}")
    public Response myFunction(@Context HttpServletRequest request
            ,@PathParam("types")String type)
    {
        Criteria criteria = session.createCriteria(myObject.class);
        String [] types = type.split(",");
        criteria.add(Restrictions.in("tranType", types));
        List<myObject> lst= (List<myObject>) criteria.list()
    }

Let me know if you have other question.

Amit
  • 30,756
  • 6
  • 57
  • 88
1

Don't use arrays in uri, just put it in parameter field, like this:

@Path("/myService/types")
public Response myFunction(@Context HttpServletRequest request
                       ,@RequestParam("types[]")String[] types)
{
    Criteria criteria = session.createCriteria(myObject.class);
    criteria.add(Restrictions.in("tranType", types));
    List<myObject> lst= (List<myObject>) criteria.list()
}
sunzheng04
  • 29
  • 2
0

The problem with this kind of solution is that the framework cannot magically transform a string (even if comma delimited) in a list. You should use simply a String as parameter and then split manually inside the method.

First of all does not exist a standard about the list separator. A comma? A pipe or a semicolon? What use as separator is completely up to you.

Another problem with this kind of solution is that the url size could grow up too much, so much that your clients could have problems consuming the resource.

freedev
  • 25,946
  • 8
  • 108
  • 125