Category: Java
-
Spring’s queryForStream Method
During one of my recent Spring and Spring Boot training course, I talked about the three major ways to access relational data from Spring: Send already-worked-out SQL using the JdbcTemplate, Work with Hibernate by injecting the JPA EntityManager using the @PersistenceContext annotation, and Simply extend one of the Spring Data interfaces, like CrudRepository. I told my students how I always struggle…
-
Solving the Daily Jumble
Recently I decided to subscribe to my local newspaper (the Hartford Courant, pronounced current) again. That’s been valuable by itself, and brought some benefits I forgot about (like a comics page), but one of the unexpected side effects has been the Daily Jumble. The idea is to unscramble the clues to form words. All of…
-
Why Use Mocks?
Testing A Simple Publisher/Subscriber System With Mockito One of the challenges I find when teaching Java testing with Mockito is that the docs, while complete, don’t motivate why you want to use mocks in the first place. This post includes a simple example to show why mocking frameworks are important and where they are useful.…
-
Java 11 HttpClient, Gson, Gradle, and Modularization
This post describes a simple system that uses the new HttpClient class that comes with Java 11 to access a RESTful web service. After demonstrating basic functionality, changes are made to parse the results using Gson, and even modularize the code and have it work with Gradle. The goal is to provide an example for…
-
Java 8 Constructor Refs (In All Their Glory)
[Note: my last post announced my new book, Modern Java Recipes, is now available from O’Reilly publishers in Early Release form. As a sample, I included a discussion of the Predicate interface, one of the new functional interfaces in the the java.util.function package. In this post, I highlight constructor references, which are discussed in another…
-
Modern Java Recipes now in Early Release
My latest book, Modern Java Recipes, is now available in Early Release form! If you have a Safari account (the online book repository from O’Reilly), you can add it to your bookshelf at any time. Otherwise, you can get it directly from http://shop.oreilly.com or even at Amazon.com. There are only three chapters in the Early…
-
Days Between Dates in Java 8
I would like to address a problem that is quite topical at the moment: calculating the number of days between two events. The new Date-Time API in Java 8 makes this process much simpler than it used to be. The Date-Time API includes the interface java.time.temporal.TemporalUnit, which is implemented by the enum ChronoUnit in the…
-
Making Java 8 Groovier: A few annotated examples
In a couple of weeks, I’m giving two talks at talk at the 2016 JavaOne conference in San Francisco. One of them is called “Groovy and Java 8: Making Java Better“. I’m building examples in preparation for the conference, and as the Groovy community is good about correcting my errors in a friendly way, I…
-
Fun with Time Zones in Java 8
[Note: Revised based on suggestions in the comments.] They say that one way to identify a software developer is to whisper the word “timezone” in their ear and see if they shudder. That’s certainly true for me, though my reaction is based more on travel and trying to arrange conference calls across time zones than…