2

It may be bit silly question, but I am confuse if it is possible to receive the JSON Objects in the @FormParam in the POST method under RESTful webservice

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(@FormParam("CodeList")
List<String> codeList)
{
    List<MyObject> myobjects = null;
    try {
        //some code
    } catch (Exception e) {
        //some exception if occur 
    }
    return myobjects;
}

And what if I want to get the userdefine object in the form param.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(@FormParam("CodeList")
List<MyObject> formMyObjectList)
{
    List<MyObject> myobjects = null;
    try {
        //some code
    } catch (Exception e) {
        //some exception if occur 
    }
    return myobjects;
}

Thanks in advance.

Sudhanshu Sharma
  • 314
  • 3
  • 12
  • Well, have you tried it? Hint: It works using `@POST` anyhow. – Smutje Feb 11 '15 at 07:33
  • 1
    You will need to get it as a String and unmarshal it yourself, or write a MessageBodyReader. The former may be easier, seen [here](http://stackoverflow.com/a/27949340/2587435). Trying to have another type method parameter, you will need a custom provider. Otherwise, why do you want to accept is as a form param anyway? Using form param, you should `@Consume("application/x-www-form-urlencod")`. If you want `application/json` then just send the body as JSON – Paul Samsotha Feb 11 '15 at 07:36
  • Thanks for the response Smutje. How can I check the same by sending the entire object to REST. I have a lot of data to receive, so I do not want to use @FormParam for every individual element of the object. I would like to receive it as an entire object so I can perform the login on it. – Sudhanshu Sharma Feb 11 '15 at 07:42
  • can you provide the code of your Controller – sachingupta Feb 11 '15 at 08:07

1 Answers1

3

This is possible however you need to understand what is @FormParam, basically it receive the values from an specific html form, as you probably know @FormParam need to know what is the param you want to take from the request using @FormParam("myParam"), if you want to consume json you need to provide it. So answer is you dont need to use @FormParam to consume JSON

The above means instead of send a pair key/value properties you need to send the full json, obviously you need to generate that json using probably jquery or even javascript then sent that to your endpoint that should be something like.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(MyBean mybean){}

Where MyBean class need to have the fields that are going to be sent within the JSON. The magic here is thanks to the MessageBodyReader, you should have jackson in your classpath, you can find an example here. Also will be good idea if you read this.

Koitoer
  • 18,778
  • 7
  • 63
  • 86