Generic entity management framework enable writing rest api with minimal code and out of the box swagger docs. It supports POST, GET, PUT, PATCH, DELETE HttpMethods.
Create a simple entity management system that provides a REST endpoint to manage any entity (e.g. a product in a catalog, patient information in healthcare etc.).
The system should allow for the following capabilities:
- It should be able to adapt to any kind of entity with minimal code changes
- Adding, removing, modifying the attributes of an entity should be simple
- Reduce developement effort by 60% becuase the developer only need to give service implmenetation. Rest of controller, log, swagger docs, workflow will be take care by the framework.
- Enforces rest best practices.
@Entity
public abstract class BasicEntity implements Serializable {
static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable = false)
@ApiModelProperty(notes = "The database generated ID.")
private Long id;
@Column(name = "created_time", insertable=true, updatable=false)
@ApiModelProperty(notes = "The database generated created time.")
private Date createdTime;
@Column(name = "updated_time", insertable=false, updatable=true)
@ApiModelProperty(notes = "The database generated updated time.")
private Date updatedTime;
}
@RestController
@RequestMapping("/entities")
@Api(value="Entity Management System API", description="CRUD operations on Entity.")
public class EntityController<T extends BasicEntity>
@ApiOperation(value = "Create entity.", response = APIResponse.class)
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> createEntity(@RequestBody T entity) {
LOGGER.debug("Inside createEntity with param "+entity);
T responseEntity = entityService.createEntity(entity);
String message = "Entity created successfully.";
APIResponse<T> apiResponse = new APIResponse(message, HttpStatus.CREATED.value(), responseEntity);
LOGGER.debug(message);
return new ResponseEntity<>(apiResponse, HttpStatus.CREATED);
}
@ApiOperation(value = "Update entity with the given id.", response = APIResponse.class)
@RequestMapping(value = "{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> updateEntity(@PathVariable(value = "id") long id, @RequestBody T entity) {
LOGGER.debug("Inside updateEntity with param "+entity);
T responseEntity = entityService.updateEntity(id, entity);
String message;
if (Objects.isNull(responseEntity)) {
message = "Entity with id "+id+" not found in the database.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.BAD_REQUEST.value(), message);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
message = "Entity Updated successfully.";
APIResponse response = new APIResponse(message, HttpStatus.OK.value(), responseEntity);
LOGGER.debug(message);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@@ApiOperation(value = "Partial update entity with the given id and the updates.", response = APIResponse.class)
@RequestMapping(value = "{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> updatePartialEntity(@PathVariable(value = "id") long id, @RequestBody Map<String, Object> updates) {
LOGGER.debug("Inside updatePartialEntity with id "+id+" and updates "+updates);
T responseEntity = entityService.partiallyUpdateEntity(id, updates);
if (Objects.isNull(responseEntity)) {
String message = "Entity with id "+id+" not found in the database.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.BAD_REQUEST.value(), message);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
APIResponse response = new APIResponse("Entity partially updated", HttpStatus.OK.value(), responseEntity);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ApiOperation(value = "Delete entity with the given id.", response = APIResponse.class)
@RequestMapping(value = "{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> deleteEntity(@PathVariable(value = "id") long id) {
LOGGER.debug("Inside deleteEntity with param "+id);
entityService.deleteEntity(id);
String message = "Entity deleted successfully";
APIResponse response = new APIResponse(message, HttpStatus.OK.value(), message);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ApiOperation(value = "Display entity with the given id.", response = APIResponse.class)
@RequestMapping(value = "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> displayEntity(@PathVariable(value = "id") long id) {
LOGGER.debug("Inside displayEntity with param "+id);
T responseEntity = entityService.getEntity(id);
if (Objects.isNull(responseEntity)) {
String message = "Entity with id "+id+" not found in the database.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.BAD_REQUEST.value(), message);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
String message = "Entity "+responseEntity +" retrieved successfully.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.OK.value(), responseEntity);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ApiOperation(value = "List all entities.", response = APIResponse.class)
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = APIResponse.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<APIResponse> displayAllEntity() {
LOGGER.debug("Inside displayEntity all entity");
List<T> responseEntity = entityService.getAllEntities();
if (Objects.isNull(responseEntity) || responseEntity.isEmpty()) {
String message = "Entity not found in the database.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.BAD_REQUEST.value(), message);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
String message = "Entity "+responseEntity +" retrieved successfully.";
LOGGER.debug(message);
APIResponse response = new APIResponse<>(message, HttpStatus.OK.value(), responseEntity);
return new ResponseEntity<>(response, HttpStatus.OK);
}
- Create the Entity Class, Example Patient
@Entity
public class Patient extends BasicEntity{
@Column(name = "name", insertable=true, updatable=false)
@ApiModelProperty(notes = "Name of the patient")
private String name;
@Column(name = "address", insertable=true, updatable=false)
@ApiModelProperty(notes = "Address of the patient")
private String address;
@Column(name = "mobile", insertable=true, updatable=false)
@ApiModelProperty(notes = "Mobile number of the patient")
private String mobile;
}
- Create the Repository, Example PatientRepository
@Repository
public interface PatientRepository extends EntityRepository<Patient> {
}
- Create the Service, Example PatientService and implement the method, That's the only implementation required.
@Service
@Resource(name="patientService")
public class PatientService implements EntityService<Patient>
- Create the Rest Controller, Example PatientController
@RestController
@RequestMapping("/patients")
@Api(value="Patient Management System API", description="CRUD operations on Patient.")
public class PatientController extends EntityController<Patient> {
}
$ mvn spring-boot:run
- After starting the application Click on Swagger-home