Starting out with Spring

I have been a .NET and Node.js web developer for the last 10 years. Building sites and APIs with ASP.NET, ASP.MVC and Express.js. Recently I’ve been working on some projects that are built with Java using various different Java technologies such as JSP and JSF.

While I have been getting my head around this new stack I came across the Spring framework and really liked the way it approached building web applications. The Spring Boot project is really amazing and solves a lot of the “where do I start?” questions when using Spring.

When starting out in a new web technology there are a general set of questions I start with:

This is not a definitive list by any means, but acts as good starting point for getting my head around it. Here are my findings after a weekend of spelunking into the Spring world.

Is it free

Yes. Spring is released under the Apache License 2.0.

What tools are available

In short loads:

I played with them all and found I prefer Intellij. I already had a subscription to JetBrains as I use WebStorm a lot as well as some of their other tools, so I didn’t have to shed a load of cash for it which might put people off. NetBeans and Spring Tool Suite are both free so there is no barrier to entry.

What is the documentation like

Great. There are a number of guides available which are up to date and really helpful.

There is also an excellent YouTube channel SpringDeveloper which has lots of useful presentations.

Another website that comes up all the time when searching for information is baeldung.com.

This all makes life a lot easier when starting out. There is nothing worse than difficult to use or out of date documentation.

What version of the language is supported

At this point (June 2018) Java 10 is out, Spring is at version 5.0.6 and Spring Boot is 2.0.2. Spring requires at least Java 8 and works with Java 10.

I like this as when starting with a new technology it’s always annoying if you have to use an older version of a language or libraries.

Where do you start

start.spring.io. This tool will generate a template Spring Boot application which you can open in your IDE of choice and get coding. It allows you to pick common dependencies and include them in the pom.xml file for the project.

How do you deploy the app

What’s really nice about Spring Boot is that it builds your app into a single .jar file which includes Tomcat. This means you use the java -jar myapp.jar command, and your app will be up and running with its own internal version of Tomcat. This makes it really easy to have something that works quickly without having to become an expert in Tomcat.

How does data access work

This is a big question that I am not going to attempt to answer in detail. Basically if you add the dependency spring-boot-starter-data-jpa to your project this will add in a load of data access related tools, the main one being Hibernate. This means you have an ORM, and it’s customisable when things get more complicated that 1 object mapping to 1 table.

What logging tools are available

Logback is included as part of the spring-boot-starter-web dependency. For more details on how to tweak the settings or switch to log4j 2 see the logging how-to guide.

How to create a REST end point

See this building a RESTful web service guild. In essence decorate a class with the @Controller annotation and a method with the @ResponseBody and @GetMapping annotations e.g.

@Controller
public class HomeController {
    @GetMapping("/home")
    @ResponseBody
    public Greeting sayHello() {
        return new Greeting("Hello");
    }
}

// returns
// {
//   "message": "Hello"
// }

How to render a web page

This works in a similar way to the above. Add this dependency to the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

To use thymeleaf templates and then change the controller to be:

@Controller
public class HomeController {
    @GetMapping("/home")
    public String sayHello() {
        return "hello";
    }
}

This will render a template called hello.html.

Wrap up

I really like Spring. It provides out of the box answers to common web development questions. The documentation and the tooling are also good.

Issues encountered

There were a couple of “why does this now work?” questions when I was getting my first project setup.

Red warning text when running the project:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/Users/blah/.m2/repository/org/springframework/spring-core/5.0.6.RELEASE/spring-core-5.0.6.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

This is nothing to worry about and is caused by using Java 10 and should not be displayed in the next version of Spring.

Tests would not run

I had a project that included spring-boot-starter-data-jpa and when I ran the default test I got a big error.

I asked about it on Github and was directed to a fix. Just include this dependency:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

Again this was caused by using Java 10.