Hecklers in Development
Code, coffee, & camaraderie. Collection, unordered. ;)

Spring Data REST takes an opinionated approach to exposing Spring Data repositories via REST endpoints, covering the 80-90% use case with a minimum of code and fuss. But did you know that it provides a no-lifting-required mechanism for exposing query methods you define on those repositories as well?

 

Let’s say you create a method like this:

 

 

Referencing that bit of functionality directly is simple, just append /search/<methodName> to the collection endpoint:

 

 

 

For more information, click here to view the Spring Data REST docs. Keep coding/keep sharing!

 

Cheers,
Mark

 

P.S. – Find this useful? Click here to follow me on Twitter and be notified of future posts! And don’t forget to share this Quick Tip via the button(s) below. Thanks!

Share

Tags: , , , , ,

I’ve only been working with Pivotal Cloud Foundry since September 2015, but I’ve learned a great deal about it since then. I also realize there is much (much much) more to learn! The world doesn’t stand still, after all, and there are many stones I’ve yet to turn.

 

Sometimes I revisit something small-but-useful that I discovered long ago that I didn’t take the time to share at the time. I intend to be better about that, beginning now. If this is something you use frequently, please disregard; if there is something you use in lieu of this tip or any other, please share. 😉

 

Anyway, many times when deploying an application to Cloud Foundry, you will want to generate a unique URL to avoid “collisions”, i.e. attempting to reuse an in-use URL. In the past, you would insert a ${random-word} Expression Language, er, expression to do this, but at some point, the preferred approach changed. To accomplish this now, simply add random-route: true to your application’s manifest.yml file. It should look something like this:

 

 

This directs Cloud Foundry to generate a couple of words randomly and append them to your application name with hyphens, resulting in a URL that looks something like https://example-app-saprophagous-unkindliness.cfapps.io.

 

You can also use the command line option –random-route.

 

For more information, please refer to the Pivotal Cloud Foundry docs.

 

Hope this helps,
Mark

Share

Tags: , , , , , , ,

At Jfokus this week, I was honored to be interviewed by Stephen Chin of Nighthacking.com. We discussed the Renewable Energy system I built and developed using industrial Internet of Things (IoT) concepts and Domain Driven Design principles. The core of the system is Java SE Embedded on the IoT Gateway device, Spring Boot + Cloud Foundry (CF) for the backend services, and an HTML5/JavaScript frontend application also delivered via CF…all accessible from any device, anywhere in the world. I was pushing code and controlling operations in St. Louis from Stockholm, Sweden – smoothly and speedily.  🙂

 

Anyway, here is the video. Hope you enjoy it!

 

 

Keep coding,
Mark

Share

Tags: , , , , , , ,

It often surprises other devs when I suggest that Spring Boot is a perfect addition to the IoT developer’s toolbox. If you’re deep into IoT and have the luxury of another group providing all of your back-end and/or cloud platform code, that’s perfectly understandable. However, if you’re a full-stack developer, by choice or necessity, you need to know this! Grab a chair and a cup of coffee; I think you’ll find this interesting (and useful).

Let Me Just Put This Out There

Here is what most people imagine when they hear “IoT”:

 

Internet of THINGS

 

But this is what they should be thinking:

INTERNET of Things

 

The things get most of the attention, mindshare, and “buzz”. Why is that? Well…because gadgets are cool! Most of us chose to be developers because of the fun/interesting/obsessive parts, not because of the dull bits. And while there are usually fun and (ahem) less-fun components to everything, who can resist gadgetry in general?

 

Ah, but without connectivity, without storage, without the ability to monitor, manage, and analyze those often-remote sensors and controls…what do we have? Truthfully, not much. Something interesting, but barely so; a school science project, perhaps. The real payoff (in money and intrigue) is in what happens when you connect those “things”. Without a versatile and reliable platform behind those devices, you just have islands of misfit toys.

 

This is where Spring Boot really shines: helping you to create meaningful software at velocity. Software that is clean, concise, readable, maintainable…and built Cloud Native. We’ll come back to that last part over the next few installments, but for now, let’s focus upon building a basic platform to enable your IoT to mean something.

Choose Your Legos(TM)

Spring Boot integrates several useful capabilities into a single, coherent code framework and approach. In a nutshell, it helps a developer rapidly build stand-alone applications that incorporate and integrate with various third-party libraries that can be deployed anywhere a JAR will run. Anywhere. It’s open source, opinionated (minimal configuration, but flexible to fully accommodate edge cases), and insanely effective. Coming from a more staid environment, I found my first exposure to Spring Boot (and every one since) surprising and refreshing. If so-called enterprise software development sounds boring to you, you probably haven’t taken Boot for a test drive.

 

And that’s it! Kidding, that’s not it. But Spring Boot does bring along several goodies that streamline the effort it takes to build robust back end applications. Remember how I said it’s “opinionated”? Let’s examine a few of its carefully-chosen opinions, step through building a simple cloud-ready application for your IoT system, and then see where that takes us. Shall we?

On Your Mark, Get Set, Go!

The Spring Initializr gets our project off to a running start. There are several ways to accomplish this, but since we’re taking the simplest path possible for this first example, let’s just point our browsers to start.spring.io. Spring Boot gives you options, such as a Gradle-based build, various versions of Boot, Java/Groovy, and packaging (JAR vs. WAR), but we’ll stick to most of the defaults for our example.

 

NOTE: To see all of the choices at your disposal, simply click the “Switch to the full version.” link at the bottom of the page.

 

Here are the choices we’ll make for our example:

  • Maven Project
  • Latest non-snapshot version of Spring Boot
  • Group: org.thehecklers (feel free to use your own)
  • Artifact: iot-service
  • Dependencies: Web, JPA*, H2**, REST Repositories

* For this example, we’ll use a JPA data source, but feel free to choose a NoSQL option. Boot gives you many data source options out of the box, and of course, you can “bring your own” with a bit more effort.
** H2 is an in-memory database. While unsuitable for environments in which physical persistence is a requirement, it functions the same from a developer perspective and satisfies our demo requirements nicely for now.

 

Once we’ve made the above selections, simply click the Generate Project button to have the Spring Initializr generate a skeleton project, bundle it into a .zip file, and serve it up for download. Save it locally, unzip it, and open the project in your favorite IDE to get started coding.

Building your IoT Service

With just that little bit of effort, we already have the foundation in place for our IoT back end service. You can verify this by running the app and pointing your browser to localhost:8080. You should see the following:

“Large streams from little fountains flow, Tall oaks from little acorns grow.”

For this installment, we’ll focus on the MVP (Minimum Viable Product) needed to support our nascent IoT installation. Assuming we’ll need to track readings captured by one or more sensors, let’s define an Entity class for our Readings:

 

The annotations @Entity, @Id, and @GeneratedValue are from the Java Persistence API (JPA) standard and identify/describe the class and its Id attribute as a JPA entity and its primary key, respectively. Aside from these annotations, the Reading class is just a straightforward POJO.

 

Next, we face the daunting task of creating the following functionality:

  • REST endpoint(s) via which our devices can provide (POST) readings
  • REST endpoint(s) allowing us to retrieve (GET) readings for review, reporting, and analysis
  • Mechanism(s) for storage and retrieval of readings from our chosen data store

Spring makes this functionality easy to implement. Spring Data REST was one of the dependencies we included when we created this project (REST Repositories), and by simply extending a Spring Data repository interface specifying the Reading class and Id type and annotating our new interface as a RepositoryRestResource, the repository is exposed via a REST API. For now, this meets our needs nicely:

 

 

Re-running our application and refreshing our browser page (localhost:8080) confirms that our REST endpoint for readings is now active:

Getting there...

Getting there…

Next, let’s test functionality by emulating a device pushing readings to our IoT service. I used curl, but feel free to use whatever means you prefer to POST to a REST endpoint.

 

 

Then we verify using a vanilla curl GET:

 

 

Here we see the two readings I created in testing, returned via our GET request:

 

 

If you’ve been pair-programming with me, congratulations! You have now created your first very basic IoT service…and since we had Spring Boot build a fully self-contained “uberJAR” (bringing its own container with it), it can be deployed wherever Java is installed. Who would have thought it could be this fast (or fun)?!?

Future Articles, Future Enhancements

As we proceed, there are several topics we will expand upon, options/adjustments we will explore (WebSocket, SQL & NoSQL persistence, …), functionality we can add (security, tailored queries, visualizations, …), and of course, we’ll leverage Spring Boot’s focus upon Cloud Native Java to deploy quickly and easily to the leading open source cloud platform, Cloud Foundry. If you have comments, questions, or suggestions, please leave them below! For updates, please follow me on Twitter at @MkHeck.

 

Keep coding,
Mark

Additional Information

Spring Initializr
Spring Boot

Spring Data REST
Spring Getting Started Guides
Cloud Foundry
Pivotal Cloud Foundry
Pivotal Web Services (for free trial)

Share

Tags: , , , , , , , ,

At JavaOne last week, several people asked me how my work at Pivotal was going, and it occurred to me: I had been so busy diving into all the new toys that I hadn’t written anything about it! This is a first step toward remedying that.  🙂

 

At the risk of this post sounding like a short commercial, I’m deeply impressed with the consistency of vision and the depth and breadth of capabilities Pivotal offers innovative organizations and developers. The various Spring offerings solve real problems, and they solve them in ways that are “developer first”…ways that make sense not just conceptually, but also when pounding out code in an IDE (or vi/emacs, if you prefer). You hear a lot about an “opinionated” approach, and that could be chafing if there weren’t such flexibility to do it it your way anytime needed/desired; but the goal is always the same:

 

Offer a way to get things done cleanly and easily by following a prescribed (opinionated) approach…

…but get out of the way quickly when another approach is warranted/desired.

 

Best of both worlds.

 

With regard to Pivotal Cloud Foundry…I once heard it described as the “travel adapter” of cloud services, and that explanation is brilliant. In short, Pivotal Cloud Foundry (PCF):

  • Works with a variety of underlying infrastructures (VMware, OpenStack, Amazon, Azure, Google Cloud)
  • Runs on-premises or public
  • Provides a single, elegant API for developers, regardless of infrastructure
  • Provides a single, elegant API for developers, regardless of siting (on-premises or public cloud)
  • Provides a true Cloud Native platform: drag, drop, run

If you’ve used other cloud platforms (and not yet used Pivotal Cloud Foundry), you’re probably reading and re-reading that list with some combination of skepticism and guarded envy. Don’t believe me; believe yourself: sign up for a free 60-day trial here and kick the tires. No better way to see the future than to create a small Spring Boot application (or bring your own) and deploy it to PCF. You’ll wonder why other vendors make it so difficult when it can be so straightforward.

 

In summary, I’m working on a great team in a great company with products from the future. What’s not to love? 😀

 

Cheers,
Mark

 

For everyone I met and re-met (!) at JavaOne, thank you for your friendship and commitment to this great community! Looking forward to our paths crossing again, and looking forward to meeting new “old friends” soon.

 

Please check back here (or follow me on Twitter: @MkHeck) for periodic ponderances, quick tips, and everything in between. Keep that code flowing!

 

Share


Powered by Wordpress
Theme © 2005 - 2009 FrederikM.de, heavily modified by Mark Heckler
BlueMod is a modification of the blueblog_DE Theme by Oliver Wunder