Reputation: 1390
I am busy with a Dropwizard application and need to have an array injected to a POJO as one of the parameters of a put
method. Unfortunately the array is not properly handled which results in a Bad Request
response. To illustrate the JSON passed by the frontend looks like:
{
"name": "Jon",
"surname": "Doe",
"children": ["Chris", "Dave", "Sam"]
}
And my Java representation:
public class Person{
private String name;
private String surname;
private List<String> children;
public Person(){
}
@JsonProperty
public String getName(){
return name;
}
@JsonProperty
public void setName(String name){
this.name=name;
}
@JsonProperty
public String getSurname(){
return surname;
}
@JsonProperty
public void setsurname(String surname){
this.surname=surname;
}
@JsonProperty
public List<String> getChildren(){
return children;
}
@JsonProperty
public void setChildren(List<String> children){
this.children=children;
}
}
And in my resource class:
@PUT
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId,
Person person) {
// Do some stuff with the person
}
How can I properly handle the deserialization of the array in the JSON?
EDIT
I am using an angular front-end and I am invoking the method as follows:
function(json){
return $http({
url: API_URL.people+"/update/personID",
method: "PUT",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: json
});
}
Where the json
argument contains the name, surname and children as above.
Upvotes: 0
Views: 515
Reputation: 1390
Turns out the code I provided words like a charm. Upon further investigation I made a mistake in the javascript
object which got converted and sent as JSON
which caused the error.
Upvotes: 0
Reputation: 39196
Looks like the GET service is defined incorrectly. It shouldn't have Person
defined.
As per http
method definition, the GET http method
can't have body. So you can't have Person
as the input parameter.
If you need to send Person
to service, you may need to change the http method to POST or something else (like PUT
) based on your requirement.
@GET
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId) {
// Do some stuff with the person
}
Upvotes: 1