10/1/2016 - 4:28 PM

Spring Boot


  • jdk 1.8+
  • maven 3.0+ /gradle 2.3+
  • for maven in pom.xml include parent as
    • org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE
  • get the spring boot dependency
    • org.springframework.boot spring-boot-starter-web
  • install spring boot plugin for maven
    • org.springframework.boot spring-boot-maven-plugin

what is spring boot maven plugin

  • Spring boot maven plugin provides
    • it collects all jars in classpath and creates an uber jar. So you can easily execute your service and transport it
    • it searches for public static void main class to run from
    • it has a build in depedency resolver which will override dependencies to match dependencies compatible with spring boot. (This will override the dependency set by us, maybe a problem since we cannot be sure if spring overrode version we set)

what can i do with spring boot?

  • Spring boot automatically configures embedded tomcat (servlet container)
  • If you want jetty we can configure embedded jetty

Rest controller

public class HelloController {

    public String index() {
        return "Greetings from Spring Boot!";

  • @RestController combines @Controller and @ResponseBody to give us back


public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx =, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName : beanNames) {

  • Spring boot application combines @ComponentScan, @Configuration, @EnableSpringMvc ( if spring-webmvc is in classpath, This will setup the dispatcherServlet (Servlet over httpServlet following servelt spec to handle http request and dispatch web requests, provide mapping and handle exceptions) @EnableAutoConfiguration. (Do we really want component scan always?)
  • Note there is no web.xml , instead @SpringBootApplication uses to launch application. Its pure java. The deployment description is embedded into code and spring determines it. (maybe not recommended)

Unit tests

public class HelloControllerTest {

    private MockMvc mvc;

    public void getHello() throws Exception {
                .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
  • MockMvc it comes from spring test dependency. It sets builder classes to send http requests to dispatcher servlet. To Autowire mockMvc we need @SpringBootTest and @AutoConfigureMockMvc
  • This just sends a fake http request to dispacther servlet.. above it is sending stuff to dispatcher servlet and then aserting the response
  • note since we do not start the server , this is a unit test for resource
  • drawback is lot of annotations

Integration tests

package hello;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {

    private int port;

    private URL base;

    private TestRestTemplate template;

    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");

    public void getHello() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(),
        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
  • embedded server is started on random port, by arguments of annotation @SpringBootTest
  • we can discover the port at runtime using @LocalServerPort annotation

Management end points by spring boot

  • after adding the above dependency do the below
mvn package && java -jar target/gs-spring-boot-0.1.0.jar
  • its easy to check health of app
curl localhost:8080/health
  • add endpoints.shutdown.enabled=true to to enable /shutdown endpoint

spring-boot-starter links

spring boot starter

what is jmx

  • jmx is java management extension. It has api to get monitoring and management information for java. We can use a jmx client and query programtically monitoring information.