When client side submit a HTML form, it will send a form object to server, like:
params={username: USER_INPUT_USERNAME,
passworkd: USER_INPUT_PASSWORD}
Then, client ajax send the params object to my Jersey server.
On the server side, how can I get and parse this HTML form object data params:
@GET
@Produces({MediaType.APPLICATION_JSON})
public FormResult getFormResult(@QueryParam("params") Object params) {
//How can I define the type of the "params" I received?
//Do I need to create a Java Bean which represent the HTML form,
//and use the bean as the type of paprams? or any other way?
}
(In above code, the return type FormResult is an POJO bean which describe the result to response to client)
When I receive the params HTML form object data, how can I define the type of the params ?(above I defined it with type “Object“, which is wrong).
Do I must define a POJO bean to represent the HTML form and use that bean to describe the type of the params?? or any other way in Jersey?
(If create POJO bean for the HTML form, If there are check boxes on the HTML form, the params will be an dynamic object, only checked field will be added to the params object, which is also a problem)
Anybody can help?
The
@QueryParammaps an individual queryString parameter, so for a GET request you need to enumerate all your parameters as method arguments:If the
paramsthat you mentioned is actually one query string parameter (http://your.rest.service?params=somethingHere), you can map that to some class that has a constructor taking aString, or a staticvalueOf(String)method where you do the actual parsing.And the FormData class can look like this:
EDIT:
For checkboxes, it’s not as dynamic as it looks: they have the same name, so you’ll have one-, several- or no- value(s) associated with that name, depending on what the user checked. Therefore, a
@QueryParam("chk") String[] checkedValuesshould be enough to handle the “dynamic” aspects here (actually, ‘multi-valued’ would be a better word).