I have a Java bean defined as follows:
public class Person {
private String name;
private String surname;
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return this.surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
I've written this simple REST method and if I pass via POST:
{ "name": "John", "surname": "Doe"}
it works.
import com.google.gson.Gson;
@Path("/person")
public class PersonRestService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response getMsg(String inputJsonObj) {
Persona p = new Gson().fromJson(inputJsonObj, Person.class);
System.out.println("Name: " + p.getName());
System.out.println("Surname: " + p.getSurname());
return Response.status(200).build();
}
}
But why have I to accept a String as an input field of the method?
I've tried to accept a Person object in the getMsg method and still passing a JSONObject like this:
{ "name": "John", "surname": "Doe"}
but I got an HTTP Status 415 – Unsupported Media Type.
What is the easiest way to receive a Person object directly in the method without parsing it with Gson once inside the method?
Thanks in advance :)