Reputation: 481
I have a method that accepts JSON in POST
request. The only way to get parameters from POST
requests is using @FormParam
.
But I consume the webservice using Android and I have no HTML forms over there.
@Path("/app")
public class WebApi {
@POST
@Path("/prog")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Res method(@FormParam("name") Res name){
try{
System.out.println("Id:" +name.getId());
System.out.println("Name: "+name.getName());
return name;
}catch(Exception e){
System.out.println(e.getMessage());
return null;
}
}
}
Res
is an entity class:
@XmlRootElement
public class Res {
@XmlElement int id;
@XmlElement String name;
// Default constructor, getters and setters ommited
}
Please tell me the way to receive parameters from POST
requests.
Upvotes: 1
Views: 3783
Reputation: 130927
Definitely, form parameters is not the only way to send data in POST
requests.
@FormParam
When you use form parameters, need to consume them using application/x-www-form-urlencoded
. It doesn't matter wheter you have HTML forms in Android or not. The Content-Type
header of your request should be set to application/x-www-form-urlencoded
.
For this situation, you would have something as following in your JAX-RS resource:
@Path("/prog")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Res method(@FormParam("id") Integer id, @FormParam("name") String name) {
...
}
To consume the above defined endpoint, your request should be like:
POST /app/prog HTTP/1.1
Content-Type: application/x-www-form-urlencoded
id=1&name=Example
Please note that no Java class to wrap your parameters is necessary to receive the parameters in this situation.
@BeanParam
If you prefer using a Java class to wrap your parameters, you could have the following:
public class Res {
@FormParam("id")
private Integer id;
@FormParam("name")
private String name;
// Default constructor, getters and setters ommited
}
And your resource method will be like:
@Path("/prog")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Res method(@BeanParam Res res) {
...
}
To consume the above defined endpoint, your request should be like:
POST /app/prog HTTP/1.1
Content-Type: application/x-www-form-urlencoded
id=1&name=Example
Instead of application/x-www-form-urlencoded
, your endpoint can consume application/json
.
To do so, your endpoint method will be as following:
@Path("/prog")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Res method(Res res) {
...
}
Depending on your JSON provider, your model class can be just like:
public class Res {
private Integer id;
private String name;
// Default constructor, getters and setters ommited
}
And the JSON will be sent in the request payload with application/json
as Content-Type
:
POST /app/prog HTTP/1.1
Content-Type: application/json
{
"id": 1,
"name": "Example"
}
Upvotes: 3
Reputation: 76
If the parameter is contained in the request entity body as json you don't need to apply @FormParam annotation, normally jax-rs implementation must support an entity provider that will map the entity body to the parameter of your method. If it doesn't fit your needs you can set a custom provider.
Upvotes: 1