1

I've a method that returns all records sons of a father record (document and rows for example):

public Datasource<Son> getSonsFromParent( @FormParam(value = "idparent") Long idparent,
        MultivaluedMap<String, String> formParams) throws Exception;

I've a well known parameter, "idparent". I want to get this parameter simply using @FormParam . It's useful for exposing this method to other people: they know that a "idparent" is required.

Also, I have a number of undefined parameters for sorting and filtering my datasource, for example

 sort[0]=name

  sort[1]=surname

  filter[0][field]=name

  filter[0][operator]=equal

  filter[0][value]=Marc

The problem is : using @FormParam, the multivalue map is always empty.

How can i mix MultivaluedMap and @FormParam together ?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Daniele Licitra
  • 1,520
  • 21
  • 45
  • Take a look here. It is an answer that can fit your needs. https://stackoverflow.com/questions/8413608/sending-list-map-as-post-parameter-jersey – Mario Apr 18 '18 at 17:36

1 Answers1

2

You can't use @Formparam, MultiValuedMap, @FormDataParam,@BeanParam in the same resource method. JAX-RS wouldn't know which is the proper object to map the info comming in the request.

In any case, I don't see why you don't just use MultiValuedMap<String,String> formParams and just validate idParent.

Using @Formparam doesn't make such parameter required, you're in charge of this through your code implementation:

public Datasource<Son> getSonsFromParent( MultivaluedMap<String, String> formParams) {

  String idParent = formParams.getFirst("idparent");

  if(idParent == null || idParent.isEmpty()){
     // return .... [idParent is required] 400 Bad Request
  }
}

If you mean to expose such parameter to your clients, it also means that your documentation for this API will expose all of the parameters, not only the required one right ?

Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • 1
    I'd like to use @FormParam because it cast String to Long if my param is an integer. after that i'll check if(param == null) throw exception. FormParam was only a simpler way, but if i can't use it, i can't :) Thank you – Daniele Licitra Apr 24 '18 at 10:39