- Extract
server.zip
to the root directory of the project. The directory should look like this:- .vscode - server - apache-tomcat-8.0.47 - jre1.8.0_231 - dist - src ...
- Open the root directory with VSCode
-
Install the Extension Pack for Java. This extension requires a JDK11+ installation. You can get it here.
If vscode doesn't recognize the java project, open any
.java
file contained in thesrc/main/java
directory to kick off the Java Language Server. -
Install the Tomcat for Java extension
-
Add the following to
settings.json
- Command Palette (Ctrl + Shift + P) > Preferences: Open Settings (JSON)Note: Replace
C:/Projects
with the path where the project is located on your machineNote: The path should be absolute and point to
server/jre1.8.0_231
"java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "C:/Projects/web/server/jre1.8.0_231", "default": true } ]
-
Command Palette (Ctrl + Shift + P) > Add Tomcat Server > Select
server/apache-tomcat-8.0.47
-
Restart VSCode
-
Command Palette (Ctrl + Shift + P) > Java: Force Java Compilation > Full
Note: A successful compilation should generate a
classes
directory insidedist/WEB-INF
containing.class
files. -
Enable automatic compilation on source code changes by checking the Java > Autobuild: Enabled option in settings, or by adding the following option to
settings.json
:"java.autobuild.enabled": true
- Right click
dist
and selectRun on Tomcat Server
- Access the project root at
http://localhost:8080
- Test the REST API at
http://localhost:8080/api/products
-
Right click
dist
and selectDebug on Tomcat Server
-
Access the project root at
http://localhost:8080
-
Test the REST API at
http://localhost:8080/api/products
-
Change some code in
src/main/java
-
Save the changes, then hit the โก icon in the debugging toolbar to hot-reload the code into Tomcat.
Note: Due to the nature of the JVM not all types of changes can be hot-reloaded. If this happens recompile the project and debug again.
-
The changes should be reflected in the next request.
-
Test the debugger by inserting a breakpoint anywhere in the source code.
-
Always stop the tomcat server before exiting VSCode. Otherwise tomcat will not release the port
8080
and you will not be able to run it again.You can stop it by using Command Palette (Ctrl + Shift + P) > Stop Tomcat Server.
If this does not work you can use the command
./catalina.bat stop
insideserver/apache-tomcat-8.0.47/bin
-
The controllers use GSON to serdes api requests and responses
- The api base path
/api
is defined with
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig { }
- Register dependency injection classes with
register(ProductsService.class);
register(new AbstractBinder() {
@Override
protected void configure() {
bindAsContract(ProductsService.class).in(Immediate.class);
}
});
- Register the package for controller resolution, the server won't start without this (change this if you change the controllers package name):
packages("controllers");
server/apache-tomcat-8.0.47/conf/server.xml
- You can change the api port here
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />