This application is developed though REST API.
PhoneBook model class
This is the phonebook POJO class
package com.sudas.study.glassfish.StudyProject.phonebook;
public class PhoneBookModel {
String phoneNumber;
String firstName;
String lastName;
String email;
public PhoneBookModel() {
// TODO Auto-generated constructor stub
}
public PhoneBookModel(String phoneNumber, /*String firstName,*/ String lastName, String email) {
super();
this.phoneNumber = phoneNumber;
// this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Stage - I
Here I am creating different phonebook model object and passing parameters through argument constructor in phonebook class.
The GET method is used to get all the phone numbers
The POST method is used to add more phone numbers
package com.sudas.study.glassfish.StudyProject.phonebook;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/phonebook")
public class PhoneNumber {
static Map<Integer, PhoneBookModel> phMap = new HashMap<>();
// this is a stub which creates a map
public static void dbStub() {
phMap.put(001, new PhoneBookModel("9527620368", "Das", "subhra.s.das@gmail.com"));
phMap.put(002, new PhoneBookModel("9730040163", "Das", "pranatidas2004@gmail.com"));
}
@GET
@Produces(MediaType.APPLICATION_JSON)
// this method returns all the phone number added in dbStub
public static List<PhoneBookModel> getAllPhoneNumbers() {
dbStub();
List<PhoneBookModel> phoneList = new ArrayList<>(phMap.values());
return phoneList;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public static List<PhoneBookModel> addPhoneNumber(PhoneBookModel phmodel) {
// calling the db stub to add existing entries
dbStub();
int size = phMap.size();
System.out.println(size + 2);
// adding a new phone number to the existing map
phMap.put(size + 2, phmodel);
return getAllPhoneNumbers();
}
}
Stage - II
This class can POST array of JSON objects
package com.sudas.study.glassfish.StudyProject.phonebook;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/phonebooks")
public class PhoneNumbers {
static List<PhoneBookModel> list = new ArrayList<>();
public static List<PhoneBookModel> dbStub() {
list.add(new PhoneBookModel("9527620368", "Das", "subhra.s.das@gmail.com"));
list.add(new PhoneBookModel("9730040163", "Das", "pranatidas2004@gmail.com"));
return list;
}
@GET
@Consumes(MediaType.APPLICATION_JSON)
public static List<PhoneBookModel> getAllNumbers() {
return dbStub();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public static List<PhoneBookModel> addNumbers(List<PhoneBookModel> phoneNumbers) {
list.addAll(phoneNumbers);
return list;
}
public static void main(String[] args) {/*
dbStub();
List<PhoneBookModel> l = Arrays.asList(new PhoneBookModel("9999999999", "Das", "sharanya.das@icloud.com"));
List<PhoneBookModel> newList = addNumbers(l);
for (int i = 0; i < newList.size(); i++) {
System.out.println(newList.get(i).getPhoneNumber());
}
*/}
}
Illustrating GET and POST request through POSTMAN
No comments:
Post a Comment