Spring Boot [Rest API] - H2 Database
CRUD Operations using Spring Boot and H2 Database.
Installation
Download or Clone the repository from here:- Click Here
Import Maven based project in any of your Favourite IDE.
./mvnw spring-boot:run
Output
Open in Browser
Access all the endpoints using Swagger UI.
http://localhost:8080/swagger-ui.html
Access H2 Database.
http://localhost:8080/h2-console
Usage
PropertyService.java
addProperty() method Saving property to the in-memory database H2.
public ResponseEntity<Object> addProperty(Property property) {
Property savedProperty = propertyRepository.save(property);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedProperty.getId()).toUri();
return ResponseEntity.created(location).build();
}
retriveAllProperties() Method to get all the properties from the DB.
public List<Property> retriveAllProperties() {
return propertyRepository.findAll();
}
getPropertyByid() Method to find property by ID.
public Optional<Property> getPropertyById(Integer id) {
return propertyRepository.findById(id);
}
deletePropertyByid() Method to delete property by ID.
public String deletePropertyById(Integer id) {
propertyRepository.deleteById(id);
return "Successfully Deleted property with ID:- " + id;
}
updateProperty() method to update existing property.
public ResponseEntity<Object> updateProperty(Property property) {
Property savedProperty = propertyRepository.save(property);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedProperty.getId()).toUri();
return ResponseEntity.created(location).build();
}
PropertyController.java
@GetMapping("/property")
public List<Property> retriveAllProperties() {
return propertyService.retriveAllProperties();
}
@PostMapping("/property")
public ResponseEntity<Object> addProperty(@RequestBody Property property) {
return propertyService.addProperty(property);
}
@GetMapping("/property/{id}")
public Optional<Property> getPropertyById(@PathVariable Integer id) {
return propertyService.getPropertyById(id);
}
@DeleteMapping("/property/{id}")
public void deletePropertyById(@PathVariable Integer id) {
propertyService.deletePropertyById(id);
}
@PatchMapping("/property")
public ResponseEntity<Object> updateProperty(@RequestBody Property property) {
return propertyService.updateProperty(property);
}
Configuration
application.properties
spring.jpa.show-sql = true
# Enabling H2 Console
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true
# Enable Web Access
spring.h2.console.settings.web-allow-others=true
Top comments (0)