Sunday, May 28, 2017

Angular2

I'm feeling a little weak on the client side these days so I'm looking into Angular.  I watched Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch and completed the Tour of Heroes tutorial.

This is a big tutorial.  I spent something like a day on this working half an hour here and an hour there over the course of about 2 weeks.  

There was one little bug in it towards the end, unresolved dependency on angular-in-memory-web-api which can be fixed by running

npm install angular-in-memory-web-api --save


There's a lot to this and so far I only scratched the surface.

Angular reminds me a bit of Tapestry with its use of components made up of templates, CSS and code.

Speaking of components, what ever happened to Web Components?  3 years ago I wrote that within a year all web development was going to be done with web components but I feel like you don't hear much about it these days.

Wednesday, May 10, 2017

Vaadin

They had a pretty good presentation at Boulder Java User's Group yesterday about Vaadin framework.

This reminded me of a framework I used years ago called ICEFaces with a similar approach of providing access to dynamic, client-side functionality while shielding the developer from having to work directly with JavaScript.  The architecture is very similar to the approach ICEFaces uses, with a JavaScript engine running in the client's browser receiving and processing updates to the DOM from the server-side component.

Vaadin uses GWT.  I played around with GWT a long time ago but it always struck me as sort of a crutch for the JFC/Swing developer transitioning to web development, and these days it's not like there's a lot of those.  But the code in the demo app seemed pretty reasonable.  I didn't pick up on the GWT connection until someone mentioned it in the Q&A so it wasn't a big distraction or anything.

The presenter Marcus Hellberg worked through this example in about half an hour giving a sense of how easy to use and powerful this framework is.  It was nice to see someone using Spring in one of these presentations and it is cool that Vaadin is available right there in the Spring Boot Initializr.  The demo app demonstrated some low-key dynamic effects like making DOM elements appear and disappear.


Saturday, May 6, 2017

Bootstrap

So I've got this ancient relic of a web site.  I started this when I was at AT&T in 1995 and it shows.  Seriously, don't laugh at the content.  I used to try to keep it up to date but at some point other things just seemed more important.

Back about 3 years ago I read something about Bootstrap and thought, oh, that looks cool, I'll have to give that a try, then forgot all about it.  Until today.

A key feature of Bootstrap is that it's mobile-friendly, in fact, mobile-first so you should be able to resize the above page or even view it on your phone and it will stack the columns and otherwise make it look reasonable on the smaller screen.

So here is my foray into Bootstrap.  See especially the links page which uses the cool dynamic tabs feature.

Bootstrap allows you to work at a higher level, focusing more on your content and not so much on the ins and outs of HTML, CSS and JavaScript.  I'm actually looking for a way to start easing back into JavaScript but this framework really insulates you from it, at least at this simple-minded level I'm working at so far.

Sunday, April 30, 2017

Spring Data REST

Last week I blogged about doing a RESTful Web Service in Spring.  Towards the end of that post I mentioned that there's another module in Spring Data which can automate some of this work called Spring Data REST.

Following through on that idea, I rewrote essentially the same service here using Spring Data REST.

From what I've seen so far, Spring Data REST helps you in two different ways.  It relieves you of having to write a controller, at least for basic CRUD methods, and it also enables Hypermedia As The Engine Of Application State (HATEOAS).

When I started trying to get my Spring MVC Test Framework integration tests working again, the first thing I noticed was the following test failing:

My test was expecting application/json but it's now returning the HATEAOS mime type application/hal+json.

Spring defines these in the MediaType class.  They don't seem to have one of those for application/hal+json.  I considered just not checking the mime type... But I googled a bit more and found Configuring Spring Data REST and Basic settings for Spring Data REST in the documentation.  I used the following configuration to change the default mime type of the JSON responses back to application/json:

The next thing that was different has to do with the argument to methods which operate on an existing entity.  My original code used a String "name" to identify the entity, but the Spring Data REST methods all use the "id" field.  Here's how I wrote the controller method to get a single entity by name:


But I'm not writing the controllers any more... Spring Data Rest creates a 'GET' endpoint taking an 'id' value, and it also creates one for my findByName() method in the Spring Data JPA repository interface.  Here's my tests for these two cases:


I rewrote the remaining tests to just use the id instead of the name.

One other change was needed... since Spring is now automatically generating a REST endpoint from the findByName() method in the repository interface, instead of me just calling it from the controller method, I needed to give that an annotation to instruct it how to handle the HTTP request parameter as its input:


Spring Data REST strikes me as a huge win for green field development situations where you just drop a dependency in the POM file (or Gradle build file) and, whoosh, instant REST endpoints for all the domain classes.  It might be more problematic to use this in a legacy app where the Spring Data REST endpoints might not match the conventions of the existing endpoints.  Also, the HATEOAS stuff, yeah, it's the semantic web wave of the future, but it is a bit different and might take some getting used to if this is your first exposure to it.

Friday, April 21, 2017

Java 8

Back around November, I started working problems at Hackerrank.  One of my goals was to learn and get better at Java 8.

Frequently in these problems I had a need to print out an array of integers.

Before we get started in Java, the programming language I've mostly used at work for the last 3 years or so is Groovy, doing application development in Grails, writing Jenkins jobs, and just for general scripting.  Here's how you print an array of integers (int[] a) in Groovy:

It's super-easy and concise.

When I was still working in Java 7, I wrote something like the following code:

Hackerrank sometimes uses huge arrays, like 50K or 200K, so I'm efficiently using StringBuilder.  But the whole thing is super-verbose.  Java 7 lacks anything like the join operator so I'm reduced to implementing it myself, more or less.  There's five lines of code here devoted to adding a space before every character but the first.  The Groovy solution is so concise, it hardly makes sense to write a method for it, but here it would be crazy not to.

Java 8 String.join() finally gives us a string join method like the one I was using in Perl over 20 years ago.  Cool!  Here's the way I was doing this in Java 8 for a while:

That's not bad.  It's a big improvement over Java 7.  It's actually a one-liner, though the line is kind of a long one.  It's basically doing the same thing as the Groovy code, except I need to explicitly shepherd the output through a series of type conversions.

There's still a way to make this a little bit better, though.  In order to come up with the second argument to String.join(), I'm converting my array to a stream, then doing another conversion to get a Collection.  It turns out that the Java 8 team provided a reduce method Collectors.joining() which eliminates the need for String.join().  This makes it possible to do everything in a stream instead of the awkward series of conversions I had before.

And it's a bit more concise, too.

It might be tempting to look at this one example and say 'Groovy wins.'  Well, after all, Groovy is built on top of Java such that the new features of Java 8 are available in Groovy, so, yeah, Groovy does win 😎.

My initial reaction to Java 8 lambdas was 'oh, Java is finally catching up to something Groovy has had since, what, 2007?'  But it's important to understand that streams and the implementation of the filter-map-reduce abstraction implemented by Java 8 is a new thing that didn't exist in Groovy before.  In recent years we've seen a lot of interest in languages like Scala and Clojure which support a functional style of programming in which the creation of mutable state is avoided for the sake of concurrency.  This is what the streams feature of Java 8 is all about, parallelism.

Thursday, April 20, 2017

RESTful Web Service in Spring

I've been spending a lot of time at Hackerrank lately, also getting up to speed on Java 8, but I suddenly realized I hadn't done much with Spring for a while.

So, this is maybe about an 8 hour effort over the course of 2 weeks or so.  It's a RESTful web service for Pokemons.

Writing a RESTful web service in Spring these days is not quite as easy as in Grails, but it's really pretty easy.  I just used MVC and @RestController.

For integration testing I followed the example in Building REST services with Spring.  I had glanced at the documentation for Spring MVC Test Framework before but never really used it, so this part was mostly new to me.  It lets you test MVC requests without bringing up a full Servlet container.

I'm aware of Spring Data Rest.  Here's a pretty good presentation: Advanced Spring Data REST - Oliver Gierke.  I'm getting ready to give this a try but wanted to go through the exercise of doing the REST implementation 'by hand' first.

Saturday, January 7, 2017

Ubuntu MATE 16.04

I was a solid Ubuntu guy from about 2008-2011.  I walked away mostly due to the decision to replace my beloved Gnome 2 with Unity as the default desktop.  I played around with Xubuntu, Lubuntu, even Kubuntu; I tried Gnome 3, nothing satisfied.  I wound up following the same path as a lot of other disgruntled Ubuntu users by switching to Linux Mint whose MATE desktop offers an experience very like Gnome 2.

I've been happily using Mint MATE for about 4 years.  The last Mint 17 upgrade 'Rosa' (17.3) was having some issues on my laptop, though.  The display would lock up once in a while, a condition I could clear by switching to another virtual desktop and back (CTRL-ALT-F1, ALT-F8).  But it was still a nuisance.  Another little thing that was going wrong is this release didn't seem able to support an external HDMI-connected display (worked fine on the same hardware with Windows 8.1).

I heard that Ubuntu 16 was supporting MATE so I decided to give that a try.  I've been running Ubuntu MATE 16.04 for about a month now.
The 2 display issues I was having went away, screen locking up and external display not working
Weirdly, I can change my wallpaper, but user-supplied desktop backgrounds seem to be forgotten as soon as you switch away from them, so adding a bunch of my own backgrounds to the system-supplied ones and having them all available from then on as I am used to doesn't seem to work any more.  (Update: edit wallpapers from Control Center: Appearance: Background and the changes stick).
My laptop has hybrid graphics.  The way this is intended to work is a slower Intel GPU is used most of the time, saving battery life, but for graphics-intensive applications, a faster nVidia GPU is used instead.  After some considerable effort to understand and use this new thing back in 2013, I got Bumblebee configured and learned to use optirun or primusrun for games and graphical apps.

The bumblebee packages are available from the Ubuntu repos, but as far as I can tell the distro just uses the nVidia device for everything by default.  I found that I didn't care that much about the energy savings as I rarely run on the battery for more than an hour or so, so I wound up just leaving it this way.  There is a setting in NVIDIA X Server Settings (PRIME Profiles) that allows switching GPUs on a global basis that seems adequate for controlling power consumption if I needed to.

Overall I'm pretty happy with this distro, but at some point I'll probably give Mint 18 MATE a try, also.

See: Linux Mint MATE 18 Or Ubuntu MATE 16.04