I have been following a tutorial of RestFull webservices, but i am not able to understand some concepts. Here is my PersonServiceImpl class.
@Path("/person")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonServiceImpl implements PersonService{
private static Map<Integer,Person> person = new HashMap<Integer,Person>();
@Override
@Path("/add")
@POST
public Response addPerson(Person p) {
Response response = new Response();
if(person.get(p.getId())!=null) {
response.setStatus(false);
response.setMessage("Person already exists");
}
person.put(p.getId(),p);
response.setStatus(true);
response.setMessage("Person added sucessfully ");
return response;
}
@Override
public Response deletePerson(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Person getPerson(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Person[] getAllPerson() {
// TODO Auto-generated method stub
return null;
}
}
I have made some changes in the code to produce and consume json file. This is my person class
@XmlRootElement(name = "person")
public class Person {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
And this is my Response class
@XmlRootElement
public class Response {
private boolean status;
private String message;
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getMessage() {
return "" + message;
}
public void setMessage(String message) {
this.message= message;
}
}
And here is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JAXRS-EXAMPLE</groupId>
<artifactId>JAXRS-EXAMPLE</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I want to know :
- How to print the values of person class in the response?
- How the json values are mapped to the java attributes and where? 3.If i enter a extra value in the json request what happens to that value?
The link to the tutorial: https://www.journaldev.com/9170/restful-web-services-tutorial-java