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 create a REST service on Open Liberty with IntelliJ.
You will learn how to get started with IntelliJ by building and testing a simple REST service which will expose
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 but will focus mainly on the IDE.
If IntelliJ isn’t installed, download and install it on your machine. The free community edition will suffice for this guide.
Launch IntelliJ and select Get from Version Control
and 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, choose Import
Project from external model
, select Maven
and click on Finish
.
You should see a progress bar indicating the status of the operation. Click on Open
on the welcome screen and
navigate to the directory that the project was saved and select Ok
. On the top left hand 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 bottom right of the IDE, click on Terminal
to bring up 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 top-left in the
Run/Debug Configurations
pop up window. Select Maven
in Add new configuration
.
For the Name
and in Command line
field, put liberty:run
.
For the Working directory
field, select the path or navigate to the finish
directory.
Once again, click the +
button at the top-left in the Run/Debug Configurations
pop up window. Select Maven
in Add new configuration
.
For the Name
and in Command line
field, put liberty:stop
.
Finally, click the +
button at the top-left in the Run/Debug Configurations
pop up window. Select Maven
in Add new configuration
.
For the Name
and in Command line
field, put liberty:dev
.
For the Working directory
field, select the path or navigate to the start
directory.
Click OK
to save changes.
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 liberty:run
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.
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 liberty:stop
in the pop up window.
You can also use the icons on the toolbar to toggle quickly between the different configurations that were set up above.
On the top left hand 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 liberty:dev
in the pop up window which will start the
Open Liberty server in development mode and will listen for file changes.
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 wish to debug the program. After you run liberty:dev
you can attach the IntelliJ debugger to the Open Liberty
process. If the debugger does not attach verify that you are running liberty:dev
and not liberty:run
.
From the menu bar, select Run
and click Edit Configurations
. Click the +
button at the top-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 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 liberty:stop
in the pop up window. Alternatively, type q
in the shell session where
you ran the server and then press the enter/return
key