spring boot dev tools

341 Views Asked by At

I'm writing a REST API in spring boot that generates a class, controller and repository at runtime. I'm using dev tools to recompile the class once the code is generated.

I have included the dev tools dependencies in my pom.xml and everything works fine on my local machine. However, once I have deployed to an Ubuntu server and I create a new class at runtime it generates an error when I try to hit the endpoint.

{
"timestamp": "2020-04-05T03:26:53.733+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/reg"

}

When I shutdown the Tomcat server and re-run the application the previously generated class and its controller become available and am able to hit the endpoint.

So my question is; how can I make spring boot to re-compile and scan all the generated components once class is generated at runtime without having to shutdown the server?

2

There are 2 best solutions below

0
On

spring-boot-devtools is an useful feature when working which IDE as it gives a very fast feedback loop for code changes and please be noted that the dev tools are automatically disabled when running a fully packaged application.

It is disabled when it is deployed/launched using java -jar or if it is triggered using a specific class loader. Then it is considered to be a production application.

You can flag the dependency as optional which is the best practice that prevents devtools from being applied to other modules. You can refer further details at this spring documentation,

https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools

0
On

The problem might be that on your local you use some sort of embedded Tomcat or Jetty. It's able to pick up changes un runtime.

I assume that you use Tomcat as a web server (not embedded one that comes with jar itself) since you mentioned that you have to reboot it manually. Not sure that the Tomcat web server is able to pick up class changes in runtime.

Try to look at this answer if you want your Tomcat to reload classes in runtime.