Skip to content

An introductory guide on how to create a REST application using Java EE running on Open Liberty: http://www.openliberty.io/guides/IntelliJ.html with IntelliJ

License

Notifications You must be signed in to change notification settings

meswan/guide-IntelliJ

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Creating a RESTful web service with IntelliJ

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.

What you’ll learn

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.

Installing and configuring IntelliJ

Getting started

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.

Configure the build project

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.

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 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.

Creating the application

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.

Configuring the server

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]

Building and running the application

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.

Debugging feature

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.

Testing the service

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

Running the tests

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

Great work! You’re done!

You developed a REST service in Open Liberty using IntelliJ.

About

An introductory guide on how to create a REST application using Java EE running on Open Liberty: http://www.openliberty.io/guides/IntelliJ.html with IntelliJ

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 53.9%
  • HTML 43.7%
  • Shell 2.4%