简体   繁体   中英

Integrating Spring Boot Hibernate JPA with Standalone Java Application

I have created a Spring boot application that used Spring Data JPA Hibernate provider.This application has basic crud operations for my database using spring jpa repositories.Created a service class to hit database using repository interfaces.

Now the problem is i want to call these database operation from another standalone java application which is not made in Spring. I want to call Spring boot repositories in this application instead of using JDBC which is used in the application.

I have no clue as to how do i integrate this.

Is your spring boot application a web application? If yes, you expose this as a REST service and consume this service from your stand alone application using Jersey Client or REST template to update your database record.

For eg PUT implementation may look like below.

@RestController
public class ExampleController {

    @Autowired
    private ExampleRepository repository

    @PutMapping("/api/address/{id}")
    public String updateRecord(@RequestBody Address address) {
        repository.save(address);
        return new ResponseEntity<String>(httpStatus);    

}

Your client (stand alone Java class) can use REST Template (or any rest client) to consume this API as below.

public class PutDemo {
 public static void main(String args[]) {
 RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/api/address/{id}/";
Map<String, String> map = new HashMap<String, String>();
 map.put("id", "100");
 map.put("name", "Roy");
 Address address = new Address("Henrico", "Richmond","VA");
restTemplate.put(url, address, map);
}
}

In case your spring boot app is a non-web application, you can use the fat jar as a dependency in your standalone class to execute database operations.

If you can modify the spring boot application source code, you can add spring data rest dependency to the spring application and spring boot will expose your original API as RESTful API, and then you can access this API via http in your client application. For more information, you can see the spring data jpa documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM