banner



How To Create Tomcat Java Web Service With Intellij

Tutorial: Your offset RESTful web service

This tutorial describes how to create a simple RESTful web service in IntelliJ IDEA and deploy it to the GlassFish Tomcat application server. The service will output Hello, Earth! when you access a specific URL through the web browser or otherwise send a GET asking to this URL. Use the switcher at the acme of the page for instructions for a different application server.

You will create a new Java Enterprise project, add the necessary Java code, tell IntelliJ IDEA where your GlassFish Tomcat server is located, so apply a run configuration to build the artifact, first the server, and deploy the antiquity to it.

Here is what you will need:

  • Java SE Development Kit (JDK) version 1.viii or later. You lot can go the JDK straight from IntelliJ Idea as described in Java Development Kit (JDK) or download and install it manually, for case Oracle JDK.

  • The GlassFish application server version 3.0.1 or later. You lot tin can get the latest release from the official reference implementation web site. The Web Profile subset should be plenty for the purposes of this tutorial.

  • The Tomcat awarding server version 7 or later.

  • A web browser to view your spider web application.

Create a new Java Enterprise project

IntelliJ Idea includes a dedicated wizard for creating Java Enterprise projects based on various Java EE and Jakarta EE implementations. In this tutorial, we volition create a elementary web application.

  1. From the main menu, select .

  2. In the New Project dialog, select Java Enterprise.

    New Java Enterprise project wizard
    New Java Enterprise project wizard

    Enter a proper noun for your project: RestGlassfishHelloWorld RestTomcatHelloWorld. For this tutorial, use Java 1.8 as the project SDK and select the REST service template. Don't select or add an application server, we will do information technology later. Select Maven and JUnit. Click Next to continue.

  3. In the Dependencies listing, select the following:

    • CDI

    • JAX-RS

    • Servlet

    • Eclipse Jersey Server

    • Weld SE

    New Java Enterprise project wizard
    New Java Enterprise project wizard

    Click Create.

IntelliJ IDEA creates the default project structure.

Explore the default project construction

IntelliJ Thought creates a project with some boilerplate code that you can build and deploy successfully.

  • pom.xml is the Project Object Model with Maven configuration information, including dependencies and plugins necessary for building the project.

    <?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-example" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>RestGlassfishHelloWorld</artifactId> <version>1.0-SNAPSHOT</version> <name>RestGlassfishHelloWorld</proper name> <packaging>war</packaging> <properties> <projection.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.eight</maven.compiler.source> <junit.version>5.7.1</junit.version> </properties> <dependencies> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>2.0.SP1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>ii.one.i</version> <scope>provided</telescopic> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</telescopic> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>examination</telescopic> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <telescopic>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>three.three.1</version> </plugin> </plugins> </build> </project>

    <?xml version="i.0" encoding="UTF-8"?> <projection xmlns="http://maven.apache.org/POM/iv.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-four.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.instance</groupId> <artifactId>RestTomcatHelloWorld</artifactId> <version>i.0-SNAPSHOT</version> <name>RestTomcatHelloWorld</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>ane.8</maven.compiler.target> <maven.compiler.source>one.8</maven.compiler.source> <junit.version>5.7.1</junit.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>four.0.ane</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>two.34</version> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.34</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-cdi2-se</artifactId> <version>2.34</version> </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se-core</artifactId> <version>3.1.8.Terminal</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <telescopic>examination</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <telescopic>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> </plugin> </plugins> </build> </project>

  • The HelloResource class is a root resources grade, which uses the following JAX-RS annotations to implement the RESTful web service:

    • The @Path notation identifies the URI for accessing this resource, relative to the application root.

    • The @GET annotation indicates that the hello() method will process HTTP GET requests to the specified URI.

    • The @Produces note specifies the MIME media type that the method produces and returns.

    package com.example.RestGlassfishHelloWorld; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello-globe") public course HelloResource { @Go @Produces("text/plain") public String hello() { render "Hello, World!"; } }

    bundle com.example.RestTomcatHelloWorld; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/how-do-you-do-earth") public class HelloResource { @GET @Produces("text/obviously") public Cord hello() { return "Hello, Globe!"; } }

  • The HelloApplication class is a subclass of javax.ws.rs.core.Awarding, which is used to configure the environment where the application runs REST resources divers in your resource classes. The @ApplicationPath annotation identifies the URL mapping for the awarding root.

    parcel com.example.RestGlassfishHelloWorld; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api") public class HelloApplication extends Application { }

    package com.example.RestTomcatHelloWorld; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api") public class HelloApplication extends Application { }

Configure the application server

Let IntelliJ IDEA know where the GlassFish Tomcat application server is located.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Build, Execution, Deployment | Application Servers.

  2. Click the Add button and select Glassfish Server Tomcat.

  3. Specify the path to the GlassFish Tomcat server install location. IntelliJ Thought detects and sets the name and version appropriately.

    GlassFish application server configuration
    Tomcat application server configuration

Create a run configuration

IntelliJ Thought needs a run configuration to build the artifacts and deploy them to your awarding server.

  1. From the chief menu, select .

  2. In the Run/Debug Configurations dialog, click the Add button, aggrandize the Glassfish Server Tomcat Server node, and select Local.

  3. Fix any warnings that announced at the lesser of the run configuration settings dialog.

    Run configuration warning

    Nigh likely, you lot will need to fix the post-obit:

    • On the Server tab, ready the Server Domain to domain1.

    • On the Deployment tab, add the antiquity that you want to deploy: RestGlassfishHelloWorld:war exploded RestTomcatHelloWorld:war exploded

  4. On the Server tab, set the URL to point to the root resource:

    http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hullo-world

    http://localhost:8080/RestTomcatHelloWorld_war_exploded/api/hello-world

    GlassFish run configuration done
    Tomcat run configuration done
  5. Click OK to save the run configuration.

  6. To run the configuration, press Alt+Shift+F10 and select the created application server configuration.

    Alternatively, if yous have your run configuration selected in the chief toolbar at the top, you can press Shift+F10 to run it.

This run configuration builds the artifacts, then starts the GlassFish Tomcat server, and deploys the artifacts to the server. Yous should see the respective output in the Run tool window.

Started GlassFish server and deployed application in the Run tool window
Started Tomcat server and deployed application in the Run tool window

Once this is done, it opens the specified URL in your spider web browser.

Deployed application output in the web browser
Deployed application output in the web browser

Troubleshooting

If you are using IntelliJ IDEA version 2020.2.2 or earlier, the New Project wizard will not add all of the necessary dependencies required for Tomcat. In this case, open pom.xml and add the following dependencies:

<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>two.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>ii.31</version> </dependency>

For case, in version 2020.ii.3, the generated pom.xml looks like this:

<?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>com.example</groupId> <artifactId>RestTomcatHelloWorld</artifactId> <version>1.0-SNAPSHOT</version> <proper noun>RestTomcatHelloWorld</name> <packaging>war</packaging> <backdrop> <maven.compiler.target>ane.eight</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>5.6.ii</junit.version> </properties> <dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>ii.1.1</version> <telescopic>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>bailiwick of jersey-container-servlet</artifactId> <version>ii.31</version> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <telescopic>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</telescopic> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-state of war-plugin</artifactId> <version>three.three.0</version> </plugin> </plugins> </build> </projection>

Concluding modified: 12 Apr 2022

Source: https://www.jetbrains.com/help/idea/creating-and-running-your-first-restful-web-service.html

Posted by: jacksonrien1947.blogspot.com

0 Response to "How To Create Tomcat Java Web Service With Intellij"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel