0% found this document useful (0 votes)
6 views

Assignment 5

The document describes a ProfileService that implements CRUD operations for profiles using JAX-RS. It defines GET, POST, PUT, and DELETE methods to retrieve, add, update, and delete profiles from a static in-memory database. The service returns profile data as JSON and accepts JSON for creating/updating profiles.

Uploaded by

dmitra2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 5

The document describes a ProfileService that implements CRUD operations for profiles using JAX-RS. It defines GET, POST, PUT, and DELETE methods to retrieve, add, update, and delete profiles from a static in-memory database. The service returns profile data as JSON and accepts JSON for creating/updating profiles.

Uploaded by

dmitra2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SERVICE ORIENTED ARCHITECTURE (ITMD 566)

Assignment: Profile Service


GET METHOD:

Code :

public ProfileService() {
profiles.put("Debayan", new Profile(1L, "Debayan", "Debayan", "Mitra"));
profiles.put("Dhoni", new Profile(2L, "Mahendra", "Dhoni", "Dhoni"));
}

@Path(value = "/profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {

ProfileService profileservice = new ProfileService();

@GET
public List<Profile> getProfiles() {
return profileservice.getAllProfiles();
}

OUTPUT :
Code :

@GET
@Path(value = "/{profileName}")
public Profile getprofiles(@PathParam ("profileName") String profileName) {
return profileservice.getProfile(profileName);
}

OUTPUT
POST METHOD

Code :

@POST
public Profile addProfile(Profile profile) {
return profileservice.addProfile(profile);
}

Using GET method after post :


UPDATE :

Code :

@PUT
@Path(value = "/{profileName}")
public Profile updateProfile(@PathParam ("profileName") String profileName, Profile profile) {
profile.setProfileName(profileName);
return profileservice.updateProfile(profile);
}

OUTPUT :

Profile Name “Admin” has been updated

USING GET METHOD AFTER UPDATE


DELETE :

Code :

@DELETE
@Path(value = "/{profileName}")
public void deleteProfie(@PathParam ("profileName") String profileName) {
profileservice.removeProfile(profileName);
}

GET method use to retrieve Profile name -> “Admin” before deletion
Using GET to retrieve the data of the Profile Name “Admin” after deletion :
SOURCE CODE :

 ProfileResource.java

package org.debayanmitra.itm566.debayan.resources;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.debayanmitra.itm566.debayan.model.Profile;
import org.debayanmitra.itm566.debayan.service.ProfileService;

@Path(value = "/profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {
ProfileService profileservice = new ProfileService();

@GET
public List<Profile> getProfiles() {
return profileservice.getAllProfiles();
}

@POST
public Profile addProfile(Profile profile) {
return profileservice.addProfile(profile);
}

@GET
@Path(value = "/{profileName}")
public Profile getprofiles(@PathParam ("profileName") String profileName) {
return profileservice.getProfile(profileName);
}

@PUT
@Path(value = "/{profileName}")
public Profile updateProfile(@PathParam ("profileName") String profileName, Profile profile) {
profile.setProfileName(profileName);
return profileservice.updateProfile(profile);
}

@DELETE
@Path(value = "/{profileName}")
public void deleteProfie(@PathParam ("profileName") String profileName) {
profileservice.removeProfile(profileName);
}

}
 Profile.java

package org.debayanmitra.itm566.debayan.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Profile {
private long id;
private String profileName;
private String firstName;
private String lastName;

public Profile() {
}

public Profile(long id, String profileName, String firstName, String lastName) {


this.id = id;
this.profileName = profileName;
this.firstName = firstName;
this.lastName = lastName;
this.created = new Date();
}

public long getId() {


return id;
}

public void setId(long id) {


this.id = id;
}

public String getProfileName() {


return profileName;
}

public void setProfileName(String profileName) {


this.profileName = profileName;
}

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 Date getCreated() {


return created;
}

public void setCreated(Date created) {


this.created = created;
}
private Date created;
}
 ProfileService.java

package org.debayanmitra.itm566.debayan.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.debayanmitra.itm566.debayan.database.DatabaseClass;
import org.debayanmitra.itm566.debayan.model.Profile;

public class ProfileService {


private Map<String, Profile> profiles = DatabaseClass.getProfiles();

public ProfileService() {
profiles.put("Debayan", new Profile(1L, "Debayan", "Debayan", "Mitra"));
profiles.put("Dhoni", new Profile(2L, "Mahendra", "Dhoni", "Dhoni"));
}

public List<Profile>getAllProfiles(){
return new ArrayList<Profile>(profiles.values());
}
public Profile getProfile(String profileName) {
return profiles.get(profileName);
}
public Profile addProfile(Profile profile) {
profile.setId(profiles.size() + 1);
profiles.put(profile.getProfileName(), profile);
return profile;
}
public Profile updateProfile(Profile profile) {
if(profile.getProfileName().isEmpty()) {
return null;
}
profiles.put(profile.getProfileName(), profile);
return profile;
}
public Profile removeProfile(String profileName) {
return profiles.remove(profileName);
}
}

 DatabaseClass.java

package org.debayanmitra.itm566.debayan.database;

import java.util.HashMap;
import java.util.Map;

import org.debayanmitra.itm566.debayan.model.Message;
import org.debayanmitra.itm566.debayan.model.Profile;

public class DatabaseClass {


public static Map<Long, Message> messages = new HashMap<>();
public static Map<String, Profile> profiles = new HashMap<>();
public static Map<Long, Message> getMessages(){
return messages;
}
public static Map<String, Profile> getProfiles(){
return profiles;
}
}

You might also like