Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AmiralBl3ndic/16f8b85f149640c908b8f189d2a44f1b to your computer and use it in GitHub Desktop.
Save AmiralBl3ndic/16f8b85f149640c908b8f189d2a44f1b to your computer and use it in GitHub Desktop.
A friendly guide to creating 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

  1. Start IntelliJ IDEA and from the main menu, select "Create a new project"
  2. Among the different project types that are shown, choose Java Enterprise
  3. Check the Web Application checkbox among the additional libraries (do not check "RESTful Web Service")
  4. 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

  1. Right click on your project's name in the Project tool window (file explorer)
  2. Select "Add framework support" and from the contextual menu, add Maven support
  3. Click "OK" and let the IDE add and process the new files generated for the maven project

Configure Maven (pom.xml)

  1. Locate the pom.xml Maven configuration file and replace its content with the provided pom.xml file

  2. Inside pom.xml replace the groupId 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 the groupId
  3. Inside pom.xml replace the artifactId by a significant name describing your project (project's name for example)

  4. If wanted/needed, change the version (still inside pom.xml)

  5. 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)

  1. Locate the web.xml file - it should be at web/WEB-INF/web.xml - and replace it content with the provided web.xml file
  2. 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)
  3. (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

  1. Hit Ctrl+Alt+Shift+S (Windows) or +; (MacOS) to access the Project Structure dialog
  2. Under Project Settings go to Artifacts
  3. Select the existing artifact (should be ${yourProjectName}:war exploded)
  4. 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

  1. Create a new configuration and select the application server of your choice.
  2. Ensure there is no warning or error in the configuration
  3. 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.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupid</groupId> <!-- TODO: replace this by the group id -->
<artifactId>artifactid</artifactId> <!-- TODO: replace this by the artifact id -->
<version>1.0-SNAPSHOT</version> <!-- TODO: replace this with the version -->
<properties>
<jersey.version>2.29.1</jersey.version> <!-- TODO: select the version of jersey to use (2.26 or above) -->
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>API Root</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>groupId</param-value> <!-- TODO: replace this with your own groupId -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>API Root</servlet-name>
<url-pattern>/api/*</url-pattern> <!-- TODO: replace this with your own prefered path -->
</servlet-mapping>
</web-app>
@muhamedoufi
Copy link

Thanks

@suryasmv
Copy link

suryasmv commented Feb 6, 2024

veryyyy thanks. Helped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment