How to create JAX-RS Jersey Java EE RESTful web applications in IntelliJ IDEA Ultimate Edition
A friendly guide to creating JAX-RS Jersey Java EE RESTful web applications in IntelliJ IDEA Ultimate Edition
Before you start
Ensure you have an application server installed and configured in IntelliJ IDEA (tested with Tomcat 9.0.26 and Glassfish 5.0.0)
Project creation
Creating a project in IntelliJ IDEA Ultimate Edition
- Start IntelliJ IDEA and from the main menu, select "Create a new project"
- Among the different project types that are shown, choose
Java Enterprise
- Check the
Web Application
checkbox among the additional libraries (do not check "RESTful Web Service") - Click "Next" and continue as you like through the project creation wizard
Convert the project to a Maven project
Once the project has been created and is opened in the IDE, follow these steps
- Right click on your project's name in the
Project
tool window (file explorer) - Select "Add framework support" and from the contextual menu, add
Maven
support - Click "OK" and let the IDE add and process the new files generated for the maven project
Configure Maven (pom.xml
)
- Locate the
pom.xml
Maven configuration file and replace its content with the providedpom.xml
file - Inside
pom.xml
replace thegroupId
by the one of your project- If you have a base package created, use this base package as a
groupId
- If not, create a base package in the
src/main/java
folder and use the same name for thegroupId
- If you have a base package created, use this base package as a
- Inside
pom.xml
replace theartifactId
by a significant name describing your project (project's name for example) - If wanted/needed, change the version (still inside
pom.xml
) Enable Auto Import for newly added Maven dependency if asked
<!-- Example: base package is com.example.webapi, located in the src/main/java folder and project name is "Hello World API" --> <groupId>com.example.webapi</groupId> <artifactId>Hello World API</artifactId>
Configure Jersey for JAX-RS (pom.xml
)
In pom.xml
locate the jersey.version
tag and choose the version you want to use
(latest version is highly recommended (2.29.1
at the time of writing))
Configure web application (web.xml
)
- Locate the
web.xml
file - it should be atweb/WEB-INF/web.xml
- and replace it content with the providedweb.xml
file - Replace the param-value for the jersey.config.server.provider.packages init param of the API Root servlet
with the groupId you selected (and wrote into
pom.xml
) - (Optional) Change the base url-pattern for your API as you like in the API Root servlet-mapping
Configure IntelliJ IDEA to work with your application
Artifact configuration
For IntelliJ IDEA to work as expected with your application (deployment in a local application server), follow these steps
- Hit
Ctrl
+Alt
+Shift
+S
(Windows) or⌘
+;
(MacOS) to access the Project Structure dialog - Under Project Settings go to Artifacts
- Select the existing artifact (should be
${yourProjectName}:war exploded
) - In the right pane, select all "Available Elements" and right click to add of of them into
/WEB-INF/lib
You will have to repeat those steps each time you add a dependency with Maven to ensure your project will work
Application server configuration (build & run configuration)
Now your project is all set up and should be ready for development but you have to ensure your application server is well set too
If you already have an application server set (default with the method described above)
You just have to ensure there is no warning in the application server configuration (inside IntelliJ IDEA) and that the artifact being deployed is the one you just configured
If you do not have an application server set for your project
- Create a new configuration and select the application server of your choice.
- Ensure there is no warning or error in the configuration
- Select the artifact you just configured for deployment
Conclusion
You should now be able to run your project, create and play with resources. Thanks for using this guide.