8.3.2. Initial Setup of RESTful Web Service Project

The following is procedures of initial setup of the RESTful web service project:

  • Generate RESTful web service project
  • Confirm communications of RESTful web service project

8.3.2.1. Preliminary preparations

Install one of the following for use with Launch confirmation.

  • Firefox
  • Chrome

8.3.2.2. Overview of the generated project

The overview of the project generated by this procedure is as follows.

Item Description
Project type Maven project
Project composition Single project composition
DB used H2 Database Engine (embedded in the application)
Built-in adapter
What is included in the generated project?

The following is included in the generated project:

  • Basic configuration for the RESTful web service
  • RESTful web service for communication confirmation
  • Initial configuration of the tool that operates in conjunction with Maven (is imported by referring to nablarch-archetype-parent (parent project)).

For relationship with other projects and directories, see Maven Archetype Configuration.

8.3.2.3. Create blank project

Generate a blank project using the archetypes provided by Nablarch.

8.3.2.3.1. Execute the mvn command

Use Maven Archetype Plugin(external site) to generate a blank project.

Change the current directory to the directory where the blank project (can be any directory) is to be created, and place the following file.

Batch file

After placing the file, specify the necessary parameters in the arguments and execute the bat file.

generateWebServiceProject.bat 5u23 <<groupId>> <<artifactId>> <<version>> <<package(optional)>>

The parameters configured in the above command are as follows. If you want to change the version of Nablarch, change 5u23.

Input item Description Configuration example
groupId Group ID (normally, enter the package name) com.example
artifactId Artifact ID myapp-jaxrs
version Version number 0.1.0
package Package (normally the same as group ID) com.example

Important

Item groupId and package are mapped to the Java package name. Use lowercase letters, numbers, and dots for these input values, and do not use hyphens.

If the command ends normally, a blank project is created under the current directory.

8.3.2.4. Communication confirmation

8.3.2.4.1. Automated test

The following unit tests are included in the project generated from the archetype.

Unit test classes Test content
SampleActionTest Check if a test with DB access is possible.

Execute the unit test and confirm that the blank project was successfully generated.

Execute the following command.

cd myapp-jaxrs
mvn test

Tip

“clean” and “test” of Maven used here are Built-in Lifecycle of Maven.

For information on other lifecycles that exist, see Built-in Lifecycle Bindings(external site).

If the execution is successful, the log given below will be output to the console.

(omitted)
[INFO] ----------------------< com.example:myapp-jaxrs >-----------------------
[INFO] Building myapp-jaxrs 0.1.0
[INFO] --------------------------------[ war ]---------------------------------
(omitted)
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(rest is omitted)

8.3.2.4.2. Launch confirmation

The following service is included in the generated project.

Class implementing the service Details
SampleAction

Service for communication confirmation of Nablarch functions commonly used when implementing RESTful web service.

Some services use JSON and other services use XML for the response.

Confirm that the blank project has been successfully generated by calling the service from the browser.

8.3.2.4.2.1. Launching the service

If the current directory is not yet moved to the generated project, move the directory.

cd myapp-jaxrs

Execute the following command to build the RESTful web service.

mvn compile

Execute the following command to start the application for communication confirmation of RESTful web service.

mvn waitt:run-headless

Tip

“waitt:run-headless” of Maven used in the above command example is a specification to use the run-headless goal of the waitt maven plugin.

For information on the waitt maven plugin, see waitt maven plugin (external site).

If the launch is successful, the log given below will be output to the console.

(omitted)
2020-04-28 08:46:53.366 -INFO- nablarch.fw.web.servlet.NablarchServletContextListener [null] boot_proc = [] proc_sys = [jaxrs] req_id = [null] usr_id = [null] [nablarch.fw.web.servlet.NablarchServletContextListener#contextInitialized] initialization completed.

8.3.2.4.2.2. Call a service that uses JSON for the response

Start FireFox or Chrome and enter the following URL in the address bar.

http://localhost:9080/find/json ("/" is not required at the end as shown)

If successful, the browser will display a JSON format response as shown below.

[{"userId":1,"kanjiName":"名部楽太郎","kanaName":"なぶらくたろう"},{"userId":2,"kanjiName":"名部楽次郎","kanaName":"なぶらくじろう"}]

Tip

If Internet Explorer 11 is used instead of FireFox or Chrome, a confirmation message asking if you want to download it will be displayed.

8.3.2.4.2.3. Call a service that uses XML for the response

Start FireFox or Chrome and enter the following URL in the address bar.

http://localhost:9080/find/xml ("/" is not required at the end as shown)

If successful, the browser will display an XML format response as shown below.

<userList>
  <sampleUser>
    <kanaName>なぶらくたろう</kanaName>
    <kanjiName>名部楽太郎</kanjiName>
    <userId>1</userId>
  </sampleUser>
  <sampleUser>
    <kanaName>なぶらくじろう</kanaName>
    <kanjiName>名部楽次郎</kanjiName>
    <userId>2</userId>
  </sampleUser>
</userList>

8.3.2.4.3. If the communication confirmation fails for some reason

If the communication confirmation fails for some unknown reason, the correct procedure may not have been followed in some part.

If the reason is not known, try again from Create blank project.

8.3.2.5. Supplementary notes

For information on the method of confirming the data of H2 and tools included in the blank project, see Initial Setup Procedure Supplementary Information.