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
Preliminary preparations¶
Install one of the following for use with Launch confirmation.
- Firefox
- Chrome
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:
|
For relationship with other projects and directories, see Maven Archetype Configuration.
Create blank project¶
Generate a blank project using the archetypes provided by Nablarch.
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.
Execute the following command.
mvn archetype:generate -DarchetypeGroupId=com.nablarch.archetype -DarchetypeArtifactId=nablarch-jaxrs-archetype -DarchetypeVersion=6u2
The version of Nablarch used in the above command is 6u2. If you want to change the version, change the following parameters.
Set value | Description |
---|---|
archetypeVersion | Specify the version of the archetype you wish to use. (Nablarch 6u2 or later must be specified) |
Enter project information¶
When the above command is executed, you will be asked to enter the following information about the blank project to be generated.
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.
When you have finished entering project information, Y: : will appear.
- Enter 「Y」 if you want to generate a template based on the information you have entered.
- Enter 「N」 if you wish to redo the project information entry.
If the command ends normally, a blank project is created under the current directory.
Communication confirmation¶
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)
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.
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 application for communication confirmation and then start it.
mvn jetty:run
Tip
“jetty:run” of Maven used in the above command example is a specification to use the run goal of the Jetty Maven Plugin. The compile goal, which builds the application, is executed in conjunction with “jetty:run” and does not need to be explicitly executed.
For information on the Jetty Maven Plugin, see Jetty 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.
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.
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>
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.
Configure the database¶
The blank project is initially configured to use the H2 Database Engine. To change the RDBMS to be used, refer to Procedure for Changing the RDBMS used for the configuration.
In addition, to generate and execute DDL from ER diagrams and automatically generate entity classes, you need to initialize and run gsp-dba-maven-plugin. For details, see Initial Configuration Method of gsp-dba-maven-plugin (DBA Work Support Tool).
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.