Saturday, March 19, 2016

NetBeans Java EE Tip #6: Create WebSocket Endpoint

The introduction of WebSocket support with the release of Java EE 7 provided a means for utilizing full duplex communication in your Java EE applications.  To utilize WebSockets, one must create an endpoint to which the client can connect for communication purposes.  NetBeans provides an easy-to-use wizard for developing WebSocket endpoints with a few clicks.

1)  Create your endpoint class.  First, select "New"->"Web"->"WebSocket Endpoint":
Figure 1.  Select WebSocket Endpoint

2)  Next, name your endpoint accordingly, provide a package into which the endpoint will be created, and also provide a WebSocket URI.  This is the URI that the client will connect to in order to send a message.


Figure 2.  WebSocket Endpoint Generation

3)  The NetBeans IDE will generate the initial WebSocket endpoint code.  All that is left to do is implementation of the onMessage() method to suit your application.



I will show you how to easily generate a client to communicate with a WebSocket endpoint using NetBeans in a future Java EE tip!

Friday, March 11, 2016

NetBeans Java EE Tip #5 - Custom Bean Validation Constraints

Bean Validation was added to the Java EE platform with the release of Java EE 6.  It brings a standard means of easily performing annotation-based validation to the platform.  There are several built-in bean validation constraints such as @NotNull, @Size, and @Pattern to name a few.  Please take a look at the Java EE documentation for a complete listing.

Sometimes there is a need to generate a custom bean validation constraint to suit the needs of an application.  In such cases, one must generate a custom validator class, as well as an annotation class for use with the validation class.  NetBeans makes it simple to do so, as it provides a wizard that produces skeleton classes that suit the specified criteria for your constraint.

To get started, right-click on the "Source Packages" node of your project or on an existing package, and choose "New"->"Bean Validation"->"Validation Constraint" (Figure 1).
Figure 1:  Select "Validation Constraint"

Next, choose a class name for the bean validation annotation class.  The name you elect to use will also be used for the annotation.  Provide a package name into which you wish to have the bean validation class created.  If you wish to have NetBeans automatically generate a validator class, then select the checkbox indicating as such.  If you elect to have the validator class generated, then provide a validator class name and a Java type to validate (Figure 2).

Figure 2:  Provide Class Name and Options

Once you click finish, the bean validation annotation class will be generated, as well as the validator class, if chosen.  You can then customize the class, as needed, to implement the validator.


Figure 3:  Generated Bean Validation Annotation Class

Figure 4:  Generated Validator Class with Custom Implementation

Once you've implemented the validator, it is ready to use.  As seen in Figure 5, simply annotate the field(s) that you wish to have validated.

Figure 5:  Utilizing the Custom Bean Validation Class



Recommended Reading on Bean Validation:

http://dataverse.org/files/dataverseorg/files/bean_validation.pdf?m=1448384482


Friday, March 04, 2016

NetBeans Java EE Tip #4 - RESTful Web Service JavaScript Client

In NetBeans Java EE Tip #3, I demonstrated how easy it is to create a RESTful web service using NetBeans.  In this tip, we will take a look at how quickly one can generate a JavaScript client for an existing RESTful web service.

To get started building the JavaScript client, right-click on the "Web Pages" folder of the NetBeans Web Application project, and choose "New"->"Web Services"->"RESTful JavaScript Client" (Figure 1).

Figure 1:  RESTful JavaScript Client

Next, provide a file name for the JavaScript file that will be generated for your client.  The wizard also allows you to choose a RESTful Web Service to utilize, and your choice of UI for the front end client site (Figures 2 and 3).

Figure 2:  Name JavaScript File & Choose Web Service

Figure 3:  Choosing Web Service

In Figure 4, you can see that I chose to use the Tablesorter UI for the front end. 

Figure 4:  Resulting UI Choice

Lastly, create the HTML page that will be utilized for your client.  Simply provide the name and optionally change the path to the file that will be generated.

Figure 4:  Name the HTML file

Once the wizard completes, your new HTML and JavaScript client will be ready to deploy and use!  

Figure 5:  HTML/JavaScript Client

The IDE will automatically reference the required JavaScript libraries to generate the client.  View the HTML sources, or click on the NetBeans project's "Remote Files" section to see which libraries are used.

Figure 6:  Remote Files in use by NetBeans Project