'Moving from 'Native' Crud/Hibernate to SpringRest API

My work had contractors create an application 3-4 years ago that used Springboot and Hibernate/Crud Repos. I am now in charge of the application. Some things don't seem to be the best practices. I have expanded the application around 10X but have always done the Database connections the way I was taught.

I currently manage data via Database direct CRUD Repo 'Saves' etc.

The application is used internally at the company but has many users.

Entity/Model (cut down example):

@Entity
public class ConfirmedOrders implements Serializable{


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ORDER_ID")
private Integer orderID;

@Column(name = "ORDER_STATUS")
private String orderStatus;

public ConfirmedOrders(Integer orderID, String orderStatus){
    this.orderID = orderID;
    this.orderStatus = orderStatus;

    public Integer getOrderID() {
    return orderID;
}

public void setOrderID(Integer orderID) {
    this.orderID = orderID;
}

public String getOrderStatus() {
    return orderStatus;
}

public void setOrderStatus(String orderStatus) {
    this.orderStatus = orderStatus;
}

CRUD Repository:

public interface ConfirmedOrdersRepository extends JpaRepository<ConfirmedOrders, Integer> {

    ConfirmedOrders findByOrderID(Integer id);

}

Controller Class (example snippet):

orderID = 12;

ConfirmedOrders cO  = confirmedOrdersRepo.findByOrderID(orderID);
cO.setOrderStatus("CANCEL");
parent.confirmedOrdersRepo.save(cO);

Now I know there is nothing 'wrong' with this method but many people I have spoken to recently said it is better to do it via REST. There are over 45 tables over 3 databases and all data changing and transfering is done as above or with Native Queries where required.

Is it worth the time for security/futureproofing to port the above over to a REST way of processing?



Solution 1:[1]

Creating a REST client that allows you to do save/get/delete/update operations should be relatively easy, the fun part is converting the other services or applications to using your REST endpoints.

If you had the time to do so, there is no harm in refactoring your code to call a rest endpoint which then calls your repository "service" that actually handles the repository functionality and data validation.

You can even create the rest functionality while leaving the original application in tact until everything is tested and migrated over. Once you do that, you can version your endpoints instead of directly modifying your controllers for changes to features and functionality that would be breaking.

While I don't completely understand your question or what your current controller is actually using, make your REST controller implement the most basic functionality (e.g. GET / POST / PUT / DELETE), then call the appropriate method in your service class.

Your service class should do all the data processing /validation required and then your repository only does the CRUD operations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SpeedyTheSnail