Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Using the Junos Space SDK Plug-in > Using the EJB-REST Wizard

Using Generated Constants to Simplify Code Maintenance

Application developers need to use many string literals to refer to Media Types, API URLs, and other XML and JSON data processing elements in their Web and Web service projects. Defining these constants in each file they are using becomes inefficient and difficult to maintain. A better method is to store these important string literals in a single constant file and use that file in your code wherever needed. This approach makes it easier to change the constants, which eases code maintenance. When you create a Junos Space application, a VendorConstants file is automatically created in both the EJB and Web projects, which you can use when your code consumes these constants throughout your code.

Two constants files are created for your application, one for Java code and the other for JavaScript use. A Java file called VendorConstants.java is created in the EJB project for your application (for example, inHelloWorldEJB/src/vnd.jssdk.helloworld). A similar file named VendorConstants.js is also created in the Web project folder for your application, in the web/js subfolder (for example, in HelloWorldWeb/web/js).

These files allow you to use some important constants in the code, such as VENDOR_ID and APP_DATATYPE_PREFIX. Mostly these constants are used in the media type in the RESTified method. For example, the web service project refers to the VendorConstants.java file in the generated REST code.

Note: If you use the wizard exclusively to generate all of your REST code, you do not need to construct these URL paths and Media Type strings manually (at least for your REST web services). However, you do still need to use them in the EJB and the JavaScript code.

Avoid changing these constants. If you change a constant, verify that all URLs and media types in the application remain operational after your change.

VendorConstants.java: Constants for the Java Code

For example, the code snippet shown below lists constants declared in the VendorConstants.java file that are used in the HelloWorld reference application. Note that the highlighted Space URLs are used for the Space APIs and the others are for the media types of your own API.

The code snippet shown below indicates how the VendorConstants.APP_DATATYPE_PREFIX constant is used to construct the media types for the REST interface.

The code snippet shown below indicates how the VendorConstants.APP_DATATYPE_PREFIX constant is used to construct the media types for the JUnit code. This JUnit code tests the API, and must be able to construct both the API URL path and the media type string.

The code snippet shown below indicates how the VendorConstants.APP_URL_PREFIX constant is used to construct the URL path for the application to send the HTTP request.

VendorConstants.js: Constants for the JavaScript Code:

The code snippet shown below contains a list of constants declared in the VendorConstants.js file. These JavaScript constants are identical to some of the constants declared in the VendorConstants.java file.

These constants are used in the JavaScript (or .js code) of the Web project. The IDE uses them in the autogenerated Top Level JS file for the task by importing the VendorConstants.js file in the loadDependencies function as displayed below.

Note: The VendorConstants.js file does not appear in the Module.xml editor's JS Config section for a task because of this code import, which takes place independent from the importing specified by Module.xml.

The code snippet shown below indicates how the APP_URL_PREFIX and APP_DATATYPE_PREFIX constants defined in the VendorConstants.js file are used in the JavaScript code.