Jsonb data type integration with spring boot
Usage of postgres sql with spring-boot to create, replace and update(specific fields in jsonb data)
Technologies used
- JDK1.8
- Spring-boot
- hibernate-types-52
- Lombok
- Postgres Sql
Functionalities Exist:
- Create new jsonb field
- update specific part jsonb ( values)
- add and remove new json key values
- append to arrays in jsonb data
- Searching the data by specific jsonb filed
Service Endpoints.
/**
* This will Create new jsonb field
*/
@GetMapping("/create/{firstName}")
public void createNewUser(@PathVariable String firstName) {
userService.createNewUserWithJsonb(firstName);
}
/**
*User provides all the data
* curl -X PUT -H "Content-Type: application/json" -d '{"salary": 100000.0, "address": {"city": "columbus", "state": "ohio", "country": "usa"},
* "children": ["lory", "jack"], "phoneNumber": "333-333-0000"}' http://localhost:8080/createwithalldetails
* @return
*/
@PutMapping("/createwithalldetails")
public Users createUser(@RequestBody PersonalDetails personalDetails) {
return userService.createNewUserWithAllDetails(personalDetails);
}
/**
* This will update specific part of jsonb
this will clear out the second level cache if any being used
*/
@GetMapping("/updateaddress/{id}")
public void updateSpecificUserSalary(@PathVariable int id) {
userService.updateUserSalary(id);
}
/**
* This will update specific part of jsonb if second level caching being used so update on persistent entity
* Invloves select query before update
*/
@GetMapping("/updateaddressforsecondlevelcache/{id}")
public void updateAddressForSecondLevelCache(@PathVariable int id) {
userService.updateAddressForSecondLevelCache(id);
}
/**
* This will add to existing array (prepend the data)
*/
@GetMapping("/updatechilds/{id}")
public void addToArray(@PathVariable int id) {
userService.addToArray(id);
}
/**
* This will Remove specific element from existing array
*/
@GetMapping("/removearray/{id}")
public void removeFromArray(@PathVariable int id) {
userService.removeFromChildArray(id);
}
/**
* This will read the data which has given Salary
* http://localhost:8080/read/200000.0
*
* @return
*/
@GetMapping("/read/{salary}")
public List<Users> readySalaryContains(@PathVariable double salary) {
return userService.readSalaryContains(salary);
}
Notes: please update your db details in application.yaml file