Jersey consume JSON Example & Test with Postman
Domain Class Example Code
@XmlRootElement
public class Test {
private String Firstname;
private String Lastname;
public Test()
{
}
public Test(String firstname, String lastname)
{
this.setFirstname(firstname);
this.setLastname(lastname);
}
public String getFirstname() {
return Firstname;
}
public void setFirstname(String firstname) {
Firstname = firstname;
}
public String getLastname() {
return Lastname;
}
public void setLastname(String lastname) {
Lastname = lastname;
}
}
Ressource Class Method
@POST
@Path("/set")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String setTest(Test someName)
{
return "Hello "+ someName.getFirstname()+" nice to meet you.";
}
Hints for using Chrome Plugin "PostMan"
1. Set Content-Type of http header to "application/json" (otherwise Jersey will answer with 415 Unsupported Media Type)
2. Send RAW message body in json format:
{"firstname":"baby","lastname":"miller"}
What if i want to get this message with get type request? There is no raw option? What will my input like?
AntwortenLöschen