An open API service indexing awesome lists of open source software.

https://github.com/openliberty/draft-guide-intellij


https://github.com/openliberty/draft-guide-intellij

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

// Copyright (c) 2020 IBM Corporation and others.
// Licensed under Creative Commons Attribution-NoDerivatives
// 4.0 International (CC BY-ND 4.0)
// https://creativecommons.org/licenses/by-nd/4.0/
//
// Contributors:
// IBM Corporation
//
:projectid: intellij
:page-layout: guide-multipane
:page-duration: 30 minutes
:page-releasedate: 2020-02-23
:page-description: Learn how to use the IntelliJ IDEA to develop a microservice with Open Liberty.
:page-seo-title: Using IntelliJ IDEA to develop a RESTful Java microservice
:page-seo-description: A getting started tutorial with examples on how to use IntelliJ IDEA to develop a RESTful Java microservice with Open Liberty
:guide-author: Open Liberty
:page-tags: ['MicroProfile', 'Java EE', 'Jakarta EE', 'Maven']
:page-related-guides: ['rest-client-java', 'rest-client-angularjs', 'rest-intro']
:page-permalink: /guides/{projectid}
:repo-description: Go to the https://openliberty.io/guides/{projectid}.html[website] for the rendered version of the guide.
:common-includes: https://raw.githubusercontent.com/OpenLiberty/guides-common/master
= Using IntelliJ IDEA to develop a microservice

[.hidden]
NOTE: This repository contains the guide documentation source. To view the guide in published form, view it on the https://openliberty.io/guides/{projectid}.html[Open Liberty website].

Learn how to use the IntelliJ IDEA to develop a microservice with Open Liberty.

== What you'll learn

IntelliJ IDEA is a popular integrated development environment (IDE) created by JetBrains, used primarily for developing programs for the Java Virtual Machine (JVM).

You'll learn how to get started with IntelliJ IDEA by building and testing a simple REST service.
This service will display the JVM's system properties. The REST service will respond to `GET` requests made to the
`\http://localhost:9080/LibertyProject/System/properties` URL.

To learn more about how to create a REST service, see https://openliberty.io/guides/rest-intro.html[Creating a RESTful
web service^].

== Additional prerequisite

- *IntelliJ IDEA:* Install IntelliJ IDEA if you did not. For installation instructions of IntelliJ IDEA, refer to the official https://www.jetbrains.com/idea/download/[IntelliJ IDEA download documentation^].

== Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

[role='command']
```
git clone https://github.com/OpenLiberty/guide-intellij.git

cd guide-intellij
```

Make a note of the directory path that the project is being cloned to.

Start IntelliJ IDEA, select `Import Project`. Navigate to the directory where the project was saved and click `Open`. On the `Import Project` dialog box, choose `Import project from external model` and select `Maven` and click `Finish`.

If the directory is not automatically open, click `Open` on the welcome screen and navigate to the directory mentioned previously and select `Open`. Select `View` in the menu bar and navigate to `Tool Windows` and click `Project` to display the `Project Tool Window` at top left side. Expand the `guide-{projectid}` project. If there is no guide directory showed up in the top left side, try to reopen the IntelliJ IDEA.

The `start` directory contains the starting project that you will build upon.

The `finish` directory contains the finished project that you will build.

At the lower left of the IDE, click `Terminal` to open the console if needed. By default, it should start
in the `guide-{projectid}` directory.

== Configure the build project

From the menu bar, select `Run` and click `Edit Configurations`.

Click the `+` button at the upper left in the `Run/Debug Configurations` pop up window.
Select `Maven` under `Add new configuration`.

* In the `Name` field, put `Run finish`.
* For the `Working directory` field, navigate to the `finish` directory.
* In the `Command line` field, put `liberty:run`.

Next, repeat the above steps using the following values in the corresponding fields:

* `Name` field: `Stop finish`
* `Working directory` field: navigate to the `finish` directory
* `Command line` field: `liberty:stop`.

Run through the steps again using the following values in the corresponding fields:

* `Name` field: `Develop start`
* `Working directory` field: navigate to the `start` directory
* `Command line` field: `liberty:dev`.

Repeat one last time, using the following values in the corresponding fields:

* `Name` field: `Stop start`
* `Working directory` field: navigate to the `start` directory
* `Command line` field: `liberty:stop`.

Once the configurations are set up, the drop down menu in the topscreen toolbar can be used to quickly select different configurations to run.

=== Setting up the SDK

The `SDK` should be configured for both the `start` and `finish` projects.

From the menu bar, select `File` and click `Project Structure`. On the panel on the left, locate the `Project Settings` section and select `Project`.

In the `Project SDK` section, click `New`, choose `JDK` and specify the path to the JDK set up on your machine.

Click `OK` to save changes.

== Try what you'll build

The `finish` directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the application, you can select `Run` in the menu bar and click `Run...`. Select `Run finish`
in the pop up window and deploy it to Open Liberty.

The tool window will be activated by default, displaying a console which allows you to follow the progress of the build.

Look at the console out for the `Run finish` command. If the following statement `"CWWKF0011I: The defaultServer server is ready to run a smarter planet. The defaultServer server started in 6.228 seconds."` shows in the console, check out the service at the
http://localhost:9080/LibertyProject/System/properties[^] URL.

You are expected to see the system properties displayed in JSON format.

To stop the application, select `Run` in the menu bar and click `Run...` and then select `Stop finish` in the pop up window.

== Developing the microserivce

Select `Run` in the menu bar and click `Run...`. Select `Develop start` in the pop up window which will start the
Open Liberty server in development mode and will listen for file changes.

Check out the service at the
http://localhost:9080/LibertyProject/System/properties[^] URL.

You are expected to see `Context Root Not Found`.

== Configuring the server

If the `Project Tool Window` is not currently displayed, select `View` in the menu bar and navigate to `Tool Windows` and click `Project` to display the `Project Tool Window` at top left side. Expand the `guide-{projectid}` project and navigate to the `start` directory to begin. If there is no guide directory showed up in the top left side, try to reopen the IntelliJ IDEA.

To get the service running, the Open Liberty server needs to be correctly configured.

[role="code_command hotspot", subs="quotes"]
----
#Replace the server configuration file.#
`src/main/liberty/config/server.xml`
----

server.xml
[source,xml,linenums,role="code_column"]
----
include::finish/src/main/liberty/config/server.xml[]
----

pom.xml
[source,xml,linenums,role="code_column"]
----
include::finish/pom.xml[]
----

[hotspot=httpEndpoint file=0]`` and [hotspot=webApplication file=0]`contextRoot` is added in the replaced file to connect to the liberty server.

The context root value [hotspot=appContextRoot file=1]`context root` is defined at the `pom.xml`.

The Open Liberty server was started in development mode in the previous section and all the
changes were automatically picked up.

Check out the service at the
http://localhost:9080/LibertyProject/System/properties[^] URL.

You are expected to see the system properties displayed in JSON format.

== Debugging the microservice

You may want to debug the program. After you run `Develop start` you can attach the IntelliJ IDEA debugger to the Open Liberty
process.

From the menu bar, select `Run` and click `Edit Configurations`. Click the `+` button at the upper left in the
`Run/Debug Configurations` pop up window. Select `Remote` in `Add new configuration`. Type `Check` in the `Name` field and `7777` into the `Port` field. Click `OK` to save the changes. Select `Run` from the menu bar and click `Debug...`. Select `Check` in the pop-up window to start the debugger.

PropertiesResource.java
[source, Java, linenums, role="code_column hide_tags=comment"]
----
include::start/src/main/java/io/openliberty/guides/rest/PropertiesResource.java[]
----

In the editor open the class `PropertiesResource` which is located in the `start/src/main/java/io/openliberty/guides/rest` directory.
Click in the margin on line [hotspot=getProperty file=0]`35`. This action will set a breakpoint on that line.

Now refresh the browser at the http://localhost:9080/LibertyProject/System/properties[^] URL. The server will stop at
the breakpoint you set and you can examine the state of your JVM. Select `Run` in the menu bar and click `Resume Program` to continue testing your application.

Remove the breakpoint you set after debugging.

== Testing the microservice

You can test this service manually by starting a server and pointing a web browser at the
http://localhost:9080/LibertyProject/System/properties[^] URL. Automated tests are a much better
approach because they trigger a failure if a change introduces a bug. JUnit and the JAX-RS Client
API provide a simple environment to test the application.

You can write tests for the individual units of code outside of a running application server, or they
can be written to call the application server directly.

EndpointIT.java
[source, Java, linenums, role="code_column hide_tags=comment"]
----
include::finish/src/test/java/it/io/openliberty/guides/rest/EndpointIT.java[]
----

The test [hotspot=endpointTest file=0]`EndpointIT` class is already provided for you to test the REST service. This test class has more lines of code than the resource implementation. This situation is common.
The test method is indicated with the [hotspot=test file=0]`@Test` annotation.

=== Running the tests

Since you started Open Liberty in development mode earlier in the guide, you will be able to run tests with the `enter/return` key. To navigate back to the terminal where development mode is running, select the `Run` tab in the bottom left corner of the IDE, and then select the `Develop start` tab at the top of the terminal window. Press the `enter/return` key to run the tests. You will see the following output:

[source,role="no_copy"]
----
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.rest.EndpointIT
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.884 sec - in it.io.openliberty.guides.rest.EndpointIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
----

To see whether the tests detect a failure, add an assertion that you know fails, or change the existing [hotspot=assertosname file=0]`assertion` to a constant value that doesn't match the `os.name` system property.

When you are done checking out the service, exit development mode by selecting `Run` in the menu bar and clicking
`Run...` and then selecting `Stop start` in the pop up window. Alternatively, type `q` in the terminal where development mode is running and then press the `enter/return` key.

== Great work! You're done!

You developed a REST service in Open Liberty using IntelliJ IDEA.

== Related Links

Learn more about MicroProfile.

https://microprofile.io/[See the MicroProfile specs^]

https://openliberty.io/docs/ref/microprofile[View the MicroProfile API^]

include::{common-includes}/attribution.adoc[subs="attributes"]