Note
|
This repository contains the guide documentation source. To view the guide in published form, view it on the Open Liberty website. |
Learn how to use IntelliJ IDE to develop a microservice with Open Liberty.
You will learn how to get started with IntelliJ, an integrated development environment written in Java for developing computer software.
You will build and test a simple REST service that 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.
This guide is similar to Creating a RESTful web service.
-
IntelliJ: For installation instructions of IntelliJ, refer to the official IntelliJ download documentation.
Learn basic setup and features in IntelliJ official documentation.
Start IntelliJ, select Get from Version Control,
paste the following in the URL
field:
https://github.com/OpenLiberty/guide-intelliJ.git
and select Clone
.
Make a note of the directory path that the project is being cloned to.
When prompted if you would like to create a project, select Yes
. On the Import Project
dialog box, choose Import
project from external model
, select Maven
and click Finish
.
You should see a progress bar indicating the status of the operation. Click Open
on the welcome screen and
navigate to the directory that the project was saved and select Ok
. On the upper left side of the screen,
expand the guide-intellij
project.
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 right of the IDE, click Terminal
to open the console if needed. By default, it should start
in the guide-intellij
directory.
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
in Add new configuration
.
In the Command line
field, put liberty:run
. In the Name
field, put Run finish
.
For the Working directory
field, select the path or navigate to the finish
directory.
Once again, click the +
button at the upper left in the Run/Debug Configurations
pop up window. Select Maven
in Add new configuration
.
In the Command line
field, put liberty:stop
. In the Name
field, put Stop finish
.
Finally, click the +
button at the upper left in the Run/Debug Configurations
pop up window. Select Maven
in Add new configuration
.
In the Command line
field, put liberty:dev
. In the Name
field, put Develop start
.
For the Working directory
field, select the path or navigate to the start
directory.
Click OK
to save changes.
You can also use the icons on the toolbar to toggle quickly between the different configurations that were set up previously.
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.
Try look at the console out for the Run finish
. 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.
Select Run
in the menu bar and click Run…
and then select Stop finish
in the pop up window.
On the upper left side of the screen, expand the guide-intellij
project and navigate to the start
directory
to begin.
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 that you created at the http://localhost:9080/guide-IntelliJ/System/properties URL.
To get the service running, the Open Liberty server needs to be correctly configured.
Replace the server configuration file.
src/main/liberty/config/server.xml
server.xml
link:finish/src/main/liberty/config/server.xml[role=include]
The Open Liberty server was started in development mode at the beginning of the guide and all the changes were automatically picked up.
Check out the service that you created at the http://localhost:9080/LibertyProject/System/properties URL.
You may want to debug the program. After you run Develop start
you can attach the IntelliJ 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 7777
into
the Port field and click OK to start the debugger.
Open the class PropertiesResource
in the editor and click in the margin on the first line of the method
getProperties()
. This action will set a breakpoint on that line. This class can be found in the directory
start/src/main/java
in the package io.openliberty.guides.rest
.
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. Click Resume Program to continue testing your application.
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. In this example, you will create a test that does the latter.
EndpointIT.java
link:finish/src/test/java/it/io/openliberty/guides/rest/EndpointIT.java[role=include]
This test class has more lines of code than the resource implementation. This situation is common.
The test method is indicated with the @Test
annotation.
pom.xml
Since you started Open Liberty in development mode at the start of the guide, press
enter/return
key to run the tests. You will see the following output:
-------------------------------------------------------
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
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 finish
in the pop up window. Alternatively, type q
in the shell session where
you ran the server and then press the enter/return
key