Over a million developers have joined DZone.

Spring Boot: Fast MVC start

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

I planned to write article about Spring Boot more than a year ago. Finally I have a time and inspiration for this. So prepare yourself for 10 – 15 minutes of high quality Spring tutorial. I’m going to demonstrate Spring Boot basics with Gradle and embedded Tomcat. I use Intellij IDEA instead of Eclipse but this shouldn’t be a problem for those of you who used to Eclipse.

Introduction to Spring Boot

What’s my goal? I want to develop something very similar to on of my previous tutorials about Spring and Java configurations. It’s a good exercise to compare two different approaches for Spring development.
No doubt, most of you know what is the main aim of Spring Boot. For the rest of readers I want to say that Spring Boot makes developers happier because it takes care of configurations meanwhile developer can focus on code production. For more details read official reference.

Gradle build file


For managing dependencies and build of the project I use Gradle. Here is howbuild.gradle file looks:

buildscript {
repositories {
//Required repos
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
dependencies {
//Required dependency for spring-boot plugin
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.2.BUILD-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'spring-boot'
war {
baseName = 'companies'
version =  '0.1'
repositories {
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
//Required dependency for JSP
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'

If you are new to Gradle, I recommend you to read about it somewhere else, e.g. on official site. It’s really nice and practical tool. It can do everything what Maven do, but without XML =)

Spring Boot initialization

Now we can set up Spring Boot on java code level.

package com.companies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class, args);

That’s it, now you can start develop your business logic. Just kidding, we need to put some extra configs related to view resolving.

package com.companies.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
public class WebMvcConfig extends WebMvcConfigurerAdapter{
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
return resolver;

After you created the class published above, you can go ahead with controller development.

Controller & View

package com.companies.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
public class HelloWorldController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView hello() {
ModelAndView mav = new ModelAndView();
String str = "Hello World!";
mav.addObject("message", str);
return mav;

And corresponding view hello.jsp for the controller:

<title>Hello world page</title>

I hope it wan’t hard to repeat all these steps.

Run Spring Boot application

The last thing which we have to do in this tutorial is launch of the application. Hence I use Gradle, and in our build.gradle file I specified that the application need to be package as WAR file – I need to run build and run war file.
Here is how it looks like in IDEA:


Result you can see here localhost:8080/hello

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}