ProductController calls InventoryController to fetch inventory by product id. Here we will show how to use the inventory controller client to support service to service calls.
Perform the steps described in section Config Data Source as you did for the other services.
To set the service port (the default port 8080 has been taken by OrderController), open the application.properties file and add the property server.port=8082
Open the file src/main/java/com/oms/productcontroller/ProductControllerApp.java and add the annotation @EntityScan("com.oms.entity")
and @ComponentScan("com.oms")
with the required import to the application class as you did for Order Controller.
In the ProductService class, change the import statement: import javax.transaction.Transactional;
to: import jakarta.transaction.Transactional;
Open the class com.oms.service.ProductService. You should see errors related to InventoryService since there is no such class in ProductController
Open the pom.xml file of ProductController and add the dependency InventoryController-client
<dependency>
<groupId>com.oms</groupId>
<artifactId>InventoryController-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Go back to ProductService.java and remove the inventoryService member along with all references to it (getter and setter)
Go to getProductInventory and change the implementation to use the inventory controller client:
public Inventory getProductInventory(String pid) {
logger.log(this.getClass().getName());
Inventory inv = null;
try {
InventoryService inventorySerice = new InventoryService("http://localhost:8081/inventoryService");
inv = inventorySerice.fetchInventory(pid);
} catch (IOException e) {
e.printStackTrace();
}
return inv;
}
If needed add the import statement import com.oms.inventorycontroller.InventoryService;
(you can also use quick fix in VSCode)
Remove the import of com.oms.entity.ShippingService and any other code elements referencing ShippingService, as this was marked as dead-code in the analysis
Run the open rewrite recipes as you’ve done for the previous services. (Note: you cannot run the open rewrite services if you have compilation errors)
Compile the ProductController service (Maven clean and then Maven install). Note: you may need to run spotless to re-arrange the imports (mvn spotless:apply)
Run the OrderController service from the spring boot dashboard in VSCode (if not already running) and create orders lines by sending the request Create Multiple Orders from Postman
Run the InventoryController (if not already running) and create inventory items by sending the request Create Multiple Inventory Items from Postman
Switch to Thunder Client in VSCode and test the Products APIs in the OMS Services collection.
Optionally review the changes in the git view of VSCode and commit to your local git repo.