banner



How To Write Restful Services In Java In Intellij Step By Step

Tutorial: Your first RESTful web service

This tutorial describes how to create a uncomplicated RESTful web service in IntelliJ Thought and deploy information technology to the GlassFish Tomcat application server. The service will output Hello, World! when you access a specific URL through the web browser or otherwise send a Get asking to this URL. Use the switcher at the top of this folio for instructions for a different awarding server.

Yous will create a new Coffee Enterprise projection, add the necessary Java code, tell IntelliJ Idea where your GlassFish Tomcat server is located, then use a run configuration to build the artifact, start the server, and deploy the artifact to information technology.

Hither is what you lot volition demand:

  • Java SE Development Kit (JDK) version 1.8 or afterward. Yous can get the JDK directly from IntelliJ Idea as described in Java Development Kit (JDK) or download and install information technology manually, for case: Oracle JDK or OpenJDK.

  • The GlassFish application server version three.0.i or later. You tin become the latest release from the official reference implementation spider web site. The Web Contour subset should be enough for the purposes of this tutorial.

  • The Tomcat awarding server version seven or afterwards.

Create a new Coffee Enterprise project

IntelliJ Thought includes a defended magician for creating Coffee Enterprise projects based on diverse Java EE and Dki jakarta EE implementations. In this tutorial, we will create a elementary web application.

  1. From the main carte, select .

  2. In the New Projection 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.viii as the project SDK and select the REST service template. Don't select or add an awarding server, nosotros volition do information technology later. Select Maven and JUnit. Click Next to continue.

  3. In the Dependencies list, 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 structure

IntelliJ IDEA creates a projection 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="ane.0" encoding="UTF-8"?> <project 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/iv.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> <proper name>RestGlassfishHelloWorld</name> <packaging>state of war</packaging> <backdrop> <project.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.ane</junit.version> </properties> <dependencies> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>ii.0.SP1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>examination</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.one</version> </plugin> </plugins> </build> </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-instance" 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>RestTomcatHelloWorld</artifactId> <version>one.0-SNAPSHOT</version> <name>RestTomcatHelloWorld</name> <packaging>state of war</packaging> <properties> <projection.build.sourceEncoding>UTF-viii</project.build.sourceEncoding> <maven.compiler.target>1.eight</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>v.7.i</junit.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</telescopic> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.containers</groupId> <artifactId>bailiwick of jersey-container-servlet</artifactId> <version>2.34</version> </dependency> <dependency> <groupId>org.glassfish.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>two.34</version> </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se-core</artifactId> <version>iii.ane.eight.Final</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <telescopic>test</telescopic> </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-war-plugin</artifactId> <version>three.3.1</version> </plugin> </plugins> </build> </project>

  • HelloResource.coffee is a root resource class, which uses the following JAX-RS annotations to implement the RESTful web service:

    • The @Path annotation 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 annotation specifies the MIME media type that the method produces and returns.

    package com.case.RestGlassfishHelloWorld; import javax.ws.rs.Get; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hullo-world") public class HelloResource { @Go @Produces("text/evidently") public Cord hello() { return "Hello, World!"; } }

    bundle com.case.RestTomcatHelloWorld; import javax.ws.rs.Go; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/how-do-you-do-earth") public course HelloResource { @Get @Produces("text/plain") public String hi() { return "Hello, Earth!"; } }

  • HelloApplication.java is a subclass of javax.ws.rs.cadre.Application, which is used to configure the environment where the application runs REST resource defined in your resource classes. The @ApplicationPath notation identifies the URL mapping for the application root (past default, it is set to /api).

    package com.example.RestGlassfishHelloWorld; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Awarding; @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 awarding server

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

  1. Printing Ctrl+Alt+Due south to open the IDE settings and select .

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

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

    GlassFish application server configuration
    Tomcat application server configuration

Create a run configuration

IntelliJ IDEA needs a run configuration to build the artifacts and deploy them to your application server.

  1. From the main bill of fare, 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 bottom of the run configuration settings dialog.

    Run configuration warning

    Most likely, you will need to fix the following:

    • On the Server tab, set 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/hello-globe

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

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

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

    Alternatively, if you accept your run configuration selected in the chief toolbar at the pinnacle, y'all can press Shift+F10 to run it.

This run configuration builds the artifacts, and so starts the GlassFish Tomcat server, and deploys the artifacts to the server. You should see the corresponding 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 washed, IntelliJ IDEA opens the specified URL in your web browser.

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

If not, try openning the URL yourself: http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/how-do-you-do-world

Troubleshooting

If you are using IntelliJ Thought version 2020.2.two or earlier, the New Project sorcerer will not add all of the necessary dependencies required for Tomcat. In this case, open pom.xml and add the post-obit dependencies:

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

For example, in version 2020.2.iii, the generated pom.xml looks like this:

<?xml version="1.0" encoding="UTF-viii"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://world wide web.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.instance</groupId> <artifactId>RestTomcatHelloWorld</artifactId> <version>one.0-SNAPSHOT</version> <proper name>RestTomcatHelloWorld</name> <packaging>war</packaging> <properties> <maven.compiler.target>1.eight</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>5.6.2</junit.version> </backdrop> <dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.ane.ane</version> <telescopic>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>two.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>bailiwick of jersey-media-json-jackson</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>two.31</version> </dependency> <dependency> <groupId>org.glassfish.bailiwick of jersey.core</groupId> <artifactId>bailiwick of jersey-client</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>three.3.0</version> </plugin> </plugins> </build> </project>

Last modified: 31 May 2022

How To Write Restful Services In Java In Intellij Step By Step,

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

Posted by: greathouseinart1972.blogspot.com

0 Response to "How To Write Restful Services In Java In Intellij Step By Step"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel