Showing posts with label frameworks. Show all posts
Showing posts with label frameworks. Show all posts

Thursday, 10 July 2025

@Nonnull versus @NotNull

A quick small blog...

So, we have two (seemingly) conflicting annotations for the same thing1.

Even people who know exactly when to use what have a chance to pick the wrong one, especially with the use of Code Completion and AI nowadays.

So here's a quick rundown, but the reference below is much more in depth:

import javax.annotation.Nonnull;

@Nonnull
IDEs and Compilers use it for Static Analysis of code. (Though the RetentionPolicy is set to RUNTIME)
import jakarta.validation.constraints.NotNull;

@NotNull
Validation Frameworks (like ORMs) use it during Runtime for checks.

What about Nullable values?

import javax.annotation.Nullable;

@Nullable
IDEs and Compilers use it for Static Analysis of code.

There is no @Nullable available for jakarta.validation.constraints. By default things are nullable, unless explicitly mentioned otherwise.

When not to use @NotNull

The primary really important thing to take away from this is to not do the following:

It might work, your IDE might complain or it might not, but at runtime this will be ignored, unless this method is called by a Framework as mentioned above.

Addendum

Don't use javax.validation.constraints.NotNull. It's been superseded by Jakarta when Oracle moved Java EE to Jakarta. See [2].

Referenties

[1] Medium - Understanding @NotNull vs @Nonnull in Java: A Practical Guide to Null-Safety
https://medium.com/@mesfandiari77/understanding-notnull-vs-nonnull-in-java-a-practical-guide-to-null-safety-5e179e918f37
[2] Medium - Difference between javax.validation and jakarta.validation
https://medium.com/@ovinduuu/difference-between-javax-validation-and-jakarta-validation-df0c6dbe5cb3

Thursday, 1 June 2017

Bower

Wow. On the website for bower1, they mention the following quote:
“ ...psst! While Bower is maintained, we recommend yarn and webpack for new front-end projects!”2 3
Damn, it's hard to keep up with the advancements in Front-end Land!

References

[1] Bower - A package manager for the web
https://bower.io/
Yarn - Fast, reliable, and secure dependency management.
https://yarnpkg.com/en/
webpack MODULE BUNDLER
https://webpack.github.io/

Wednesday, 3 December 2014

REST + Angular = Restangular

JQuery and REST

I have already mentioned calling REST services using JQuery1 in my entry on REST and Error messages at my blog.

At the risk of being redundant, here it is:

Unfortunately, it requires a lot of settings and a lot of DOM manipulation to create the view and make it work as it should.

I was looking for an alternative, something that's easy to use without a lot of boilerplate.

I would prefer something like JSF, but solely on the client side, and which fully supports REST. I found something to my liking in Angular.

Angular

AngularJS is, according to their website2, a Superheroic JavaScript MVW Framework.
“AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVW, MVVM, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!”
As far as I can tell, you add Angular specific xml attributes to your html tags, and Angular takes care of the updating of those tags, depending on what's changed. Of course, that's just the tip of the proverbial iceberg.

Angular and REST

The Angular version of the JQuery $.ajax call is called $http3, and is about what you would expect.
Both in JQuery and in Angular, it is a wrapper around the XMLHttpRequest.

This is not really a REST-aware API, to say the least. Therefore Angular also provides a $resource service. This service creates resource objects that makes it easy to communicate with RESTful backends.

It is an optional module in Angular, called ngResource, and needs to be installed.

Restangular and REST

However, I have opted to make use of Restangular4. It seems to be more self-evident how it works, follows the guideline of Angular more closely and seems to abstract away a lot of the boilerplate, for example URLs.

In the example above there are numerous interesting tidbits:
  • In the config call, I set the base url. All consequent calls to REST resources have this url as root.
  • In the config call, I needed to set the "id" of the resource accessed, otherwise Restangular assumes that the "id" is a field called "id". In this case it was "name"5 6.
  • var restBase = Restangular.all("worldattributes") sets a resource (in this case for world attributes).
  • restBase.getList()
            .then(function(worldattributes) 
            {
              // returns a list of worldattributes
              $scope.worldattributes = worldattributes;
              $scope.setFilter();
              $scope.isNew = false;
            });
    This creates a GET XMLHttpRequest for url "/karchangame/resources/administration/worldattributes" and retrieves all objects. It assumes JSON. As you see, I promptly put all info into the Angular scope, so it can be reached in the HTML file (see below).
  • The objects returned by Restangular, are themselves also REST resources, therefore contain methods such as put() which updates it (causing an automatic HTTP PUT request).
  • The call remove is translated to a DELETE XMLHttpRequest.
  • And my update method decides on a POST or PUT depending on wether it is a new or a changed world attribute.
  • The Restangular.copy call is convenient, as it makes a deep copy of the object.
  • Sometimes, the backend REST urls are not standard, in which case you wish to turn from the default, and start making calls with a bit more control. An example of such is visible with the customDELETE in the disown function.
The HTML part of the Angular looks like follows:

Notice that the HTML file looks almost exactly as a simple HTML file would, besides the added Angular attributes and the references to the $scope object (by means of {{ and }}).

References

[1] JQuery.ajax() | JQuery API Documentation
http://api.jquery.com/jquery.ajax/
[2] AngularJS - Main Website
https://angularjs.org/
[3] ngbook - The Complete Book on AngularJS
Ari Lerner
[4] Github - Restangular
https://github.com/mgonto/restangular
[5] Stackoverflow - Restangular put loses part of the url
http://stackoverflow.com/questions/24262949/restangular-put-loses-part-of-the-url
[6] Restangular - GitHub : What if each of my models has a different ID name like CustomerID for Customer?
https://github.com/mgonto/restangular#what-if-each-of-my-models-has-a-different-id-name-like-customerid-for-customer
Foundation 5
http://foundation.zurb.com/