• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java Web Database Application Development with Spring Boot, JPA/Hibernate and Thymeleaf

Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an updated edition of my 2012 book “Java Web Database Application Development” (https://javawebdb.wordpress.com/) which will use SpringBoot, JPA/Hibernate and Thymeleaf. My open-source project at https://github.com/hhyde007/RADSpringBootGen features a complete sample application built this way.
With the moderator's permission I would like to preview some content here to see if the CodeRanch hands find this valuable and/or have any constructive criticism to offer.

I start out with a Chapter Zero for people with no prior Spring Boot experience. And since before working with Spring Boot one has to know the rudiments of Maven, Ant or Gradle, I start with a review of Maven.
So my actual topic for this post is Maven, if you are interested.

My proposed content is:
[COPYRIGHT 2019 by Howard Hyde. All rights reserved]

Essential Tutorials (in this order)
There are many good technical tutorials available, in fact so many that it can be difficult to nail down the ones that are most essential for one’s mission.  The ones listed below are the ones that I recommend for our present purposes, more or less in the given order.
By the way, take all the time you need.  The tutorial may state that you can learn this in 15 minutes, and if you can do it in just 5, that’s great, but if instead you really need 5 hours to get into the material hands-on and with variations, take the 5 hours without apology. I frequently think of the analogy of the musician who, even though he or she “knows” the scales, still practices them, polishes them, refines them daily, even 20 years into a professional career.

Maven Build Tool Tutorials
• https://spring.io/guides/gs/maven
• https://www.baeldung.com/maven

IF YOU DON’T KNOW MAVEN, DON’T SKIP THIS STEP! It is essential to have a basic understanding of Maven for all of your work with Spring Boot and Spring Tool Suite (STS). Maven is the build tool of choice for Spring apps (Gradle is an alternative if you or your manager prefers).

Comments on these tutorials and on Maven generally
IMHO the two tutorials listed here are tied for first place as the clearest and most concise explanations of what Maven is, what it does and how it does it that I have found. I recommend you go through both of them; they overlap on several points, but take different approaches.  Just as two eyes facilitate 3-dimensional vision, the enrichment of a second presentation of the same topic can be a force-multiplier of your understanding. Again, I wish I had seen these the first time I heard of Maven; it would have saved me a bundle in headache medication.

You won’t use every feature even of these simple getting-started tutorials right away; but they will give you a strong foundation upon which to build your skills.

If you’ve never used a build tool like Ant, Gradle or Maven before, you may be wondering, when do I execute javac (Java Compile) at the command line? The answer is, you don’t. The build tool does that for you, in this case Maven via the mvn compile and/or mvn package commands; and it handles all of the internal class and external library/JAR dependencies based on the information you provide it in your pom.xml file.

Point to highlight: “INFO: You might like to consider using the Maven wrapper to insulate your developers against having the correct version of Maven, or having to install it at all. Projects downloaded from Spring Initializr have the wrapper included. It shows up as a script mvnw in the top level of your project which you run in place of mvn.”
That’s an important point, failing which otherwise once you get into developing in Spring Tool Suite (STS) using that IDE’s project model, you may be confused as to “but where is Maven?”

Note on Dependency Management and Repositories: As a hacker coding in your parents’ garage, you are free to download, import and run libraries/JARs from their original public repositories. From your cubicle behind a corporate firewall, however, you may be limited as to what libraries you are permitted to download and/or what versions thereof, into an internal corporate repository. Your company may have policies and processes for certifying external libraries for security and compatibility purposes. The versions of individual libraries/JARS  that are Auto-configured by Spring Boot may or may not be available to you, requiring you to override the version property with a specific number that is available; and then that version of that component may be incompatible with other components, the versions of which are available to you. Headache!

Again, take your time and don’t hesitate to take the side trips for enrichment by clicking on external links, like for example the one on Semantic Versioning: https://semver.org/.

What could possibly go wrong?

Just don’t, don’t, don’t, don’t – don’t panic! The first and second [ERROR] messages are your guides to how to fix the problem, in this case, “reached end of file while parsing”.
I left the closing curly brace off the end of my HelloWorld.java listing, so I have an invalid, un-compilable class definition. You may have a different Java syntax error to fix.  In my case, a single character, a file save, and…

What could go right?

Pop Quiz: Do you know what an artifactId is?
If your answer to that question is not “Of course!” then review the tutorials above and/or read this mini-refresher:
The essential definition of your project comes in this form:

• GroupId: Group or organization creating/publishing the project, expresses as a reversed domain name
• ArtifactId: This will becom the name of your JAR (or WAR) file, comprising your complete application.

Burn those concepts into your brain; you are going to see them again and again. They apply to your project and to the external dependencies and resources that you include in your project.

Other Maven resources:
Skip these for now if you think you’ve got enough information to do your work; come back to them for enrichment or if you find yourself struggling with Maven-related issues down the road.
• https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
• https://maven.apache.org/guides/getting-started/index.html
• http://tutorials.jenkov.com/maven/maven-tutorial.html

Posts: 527
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good.  Is that the whole chapter?
Howard Hyde
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, just a bite. More to come.
Howard Hyde
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next installment, again intended for YOU to benefit from AND to critique.

Spring Boot Tutorials
Comments on Spring Boot in general
Spring Boot, rather than being separate and distinct from Spring Framework, Spring MVC etc, may be thought of as an approach to developing Spring Framework apps, a framework for a framework; a minimalist approach optimized to require the least information from the developer in order to realize the maximum functionality. It accomplishes this by being “opinionated” in the use and implementation of default configuration and functionality. If the xyz property in 99% of cases gets set to the value ‘abc’, then why require the developer to provide a value for that property?  The framework will set xyz = ‘abc’ unless otherwise specified/overridden by the developer.
If you are an experienced Spring Framework developer, then you will likely find Spring Boot to be an extremely convenient labor reducer and productivity tool. On the other hand, if you’ve never done any Spring at all before, then you will also find Spring Boot to be an extremely convenient labor reducer and productivity tool, but in the longer run you will need to know more about what it is actually doing for you under the covers. Superficial knowledge of how to push the buttons, cookbook-fashion, will be inadequate when things go wrong, when you need to debug complex programs. An experienced Spring Framework developer will have a much easier time because he or she understand the lower levels.

We will defer a deeper discussion of Dependency Injection and Inversion of Control, key concepts and features of Spring applications, until after we have gotten our feet wet.

The Tutorials
Below are the recommended tutorials for Spring Boot. The first one gives you options of building with Gradle, Maven or the Spring Tool Suite (STS) IDE, which is based on Eclipse. I recommend that unless you know you are going to be working with Gradle, you may skip that part for now and go with Maven; but definitely do that before you build with STS; and definitely build with STS after that because that’s likely to be your most common development environment and tool going forward for all but the most trivial Spring Boot applications.
If you’ve not worked with Java annotations much before, you may freak at all the @SpringBootApplication, @Bean, @Autowired, @Component and suchlike that you encounter in Spring and Spring Boot applications. For your convenience, here are a couple of basic tutorials on annotations.
• https://www.baeldung.com/java-default-annotations
• https://beginnersbook.com/2014/09/java-annotations/

Here are the main recommended introductory tutorials on Spring Boot.
• https://spring.io/guides/gs/spring-boot/
• https://www.baeldung.com/spring-boot-start
• https://www.mkyong.com/tutorials/spring-boot-tutorials/
And the following, which is linked from the previous, shows you how to set up any starter project in STS.
• https://spring.io/guides/gs/sts/

Speaking of “Starters”...
Comments on “Building an Application with Spring Boot”
The tutorial mentions, casually, almost in passing, that your pom.xml needs the entry:

Make no mistake: That’s huge! With that one little entry comes enough materiel to provision an expeditionary army. The official description for that artifact is, “Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container”.  So all of those resources are now available and included for you; a complete set of web development tools and technologies, for the price of typing fewer than 150 bytes. You don’t have to download, install and configure a webserver, you just get one free of charge or effort, embedded and full-featured.
How does it do all of that? Well, the key word there is “starter”.  Starters are dependencies that contain or reference other dependencies, which are included in the package for you without you having to spell them all out in your app’s pom.  So for example, spring-boot-starter-web has a pom.xml of its own with the following dependency entries:

And since you see the word “starter” in some of those entries, you understand that the pom for each of those lists more dependencies of their own which aggregate to provide all of the functionality needed for that topic.
So a single starter entry may save you the trouble of including dozens or even hundreds of dependency entries in your pom.
One ramification of this is that if someone asks you, do you know Spring MVC? – and you say, No, I only know Spring Boot, if your Spring Boot app has the spring-boot-starter-web entry in its pom and you are utilizing the functionality provided by the included JARs, then Yes, you ARE a Spring MVC developer. Put another way, once you dip your toe into the Spring ecosystem, any and all aspects of it are your domain.

One Very Special Starter
…is <spring-boot-starter-parent>.
This is the starter that brings in all of the dependencies/libraries that you need to spring up a Boot application, or boot up a Spring application, depending on your preference of puns.

Not all elements that you included in your pom.xml, or in your Java app, are hard-coded literals; some or many may be resolved at runtime by properties in the system or in a .properties file.  As you read tutorials on Maven and Spring, you’re eventually going to notice elements preceeded by a dollar sign and wrapped in curly braces, like our fictitious heading ${maven-property}.
According to https://maven.apache.org/pom.html,
Properties are the last required piece to understand POM basics. Maven properties are value placeholder[s], like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property.
They come in five different styles:
1. env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH environment variable.
Note: While environment variables themselves are case-insensitive on Windows, lookup of properties is case-sensitive. In other words, while the Windows shell returns the same value for %PATH% and %Path%, Maven distinguishes between ${env.PATH} and ${env.Path}. As of Maven 2.1.0, the names of environment variables are normalized to all upper-case for the sake of reliability.
2. project.x: A dot (.) notated path in the POM will contain the corresponding element's value. For example: <project><version>1.0</version></project> is accessible via ${project.version}.
3. settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example: <settings><offline>false</offline></settings> is accessible via ${settings.offline}.
4. Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as ${java.home}.
5. x: Set within a <properties /> element in the POM. The value of <properties><someVar>value</someVar></properties> may be used as ${someVar}.

For a more detailed exploration of Maven properties, see this article:
• https://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html

COPYRIGHT 2019 by Howard Hyde. All Rights Reserved.
Howard Hyde
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have more installments to this in the pipeline, but I need to know that there is interest. Please post a reply or give me some points if you are finding this worthwhile.
Curse your sudden but inevitable betrayal! And this tiny ad too!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic