New Features in Spring Boot 1.4

On Last month May 17, 2016 spring boot team has announced the release of Spring Boot 1.4.0 M3. There is lot of changes coming up in the new release. In this post I am going to list down the summary of new features in spring boot 1.4 This release closes over 150 issues and introduces a number of new features.

Highlights include:
  • Lots of bug fixes and improvements over M2 (thanks to everyone for trying the milestones).
  • Convention based error pages (want a custom 404, just add src/main/resources/public/error/404.html).
  • Improved ErrorPage registration support.
  • Support for pluggable OAuth2 Principal extraction.
New Features in Spring Boot 1.4
There are following new features of Spring Boot 1.4 Version release
  1. Executable JAR Layout
  2. Startup error improvements
  3. Hibernate 5
  4. Spring Framework 4.3
  5. Third Party Library
  6. Custom JSON Serializer and Deserializer
  7. New auto-configuration support
    1.     Couchbase
    2.     Neo4j
    3.     Narayana transactional manager
    4.     Caffeine Cache
  8. Actuator improvements
  9. Testing improvements
  10. Datasource binding
  11. Image Banner
  12. Summary

Executable JAR Layout
If you are building the JAR file as an executable spring boot application, all the dependencies will be packaged under WEB-INF/lib and application’s own classes will be packages under root of the JAR file. But, spring boot 1.4.0 will package the dependencies under BOOT-INF/lib and application’s own classes under BOOT-INF/classes.

Startup Error Improvements
There is a improvement in displaying the useful error description on startup failures. Prior to 1.4.0, startup failures shows long stack trace that would not show the actual issue unless developer has to read the complete stack trace. But, latest release just shows the cause of the failure and what is the solution.

Hibernate 5
If you have upgrade problems switching to Hibernate 5 can be postponed at a later stage by setting the hibernate.version in your pom.xml. Note though that the Spring team will not support Hibernate 4.3 from Spring Boot 1.4. So, it is advisable to upgrade to the latest hibernate versions immediately.
If you want to still use the older version in your application, please add the following entries in your pom.xml:
<properties>
 <hibernate.version>4.3.11.Final</hibernate.version>
</properties>

Spring 4.3.0-RC1
In Spring 4.3.0-RC1 there are lots of new features and enhancements. The full list you can find it here. (Also Read : Spring 4 Tutorials).

Third Party Libraries
  • Hibernate 5.1
  • Jackson 2.7
  • Solr 5.5
  • Spring Data Hopper
  • Spring Session 1.2
  • Hazelcast 3.6.
Custom JSON Serializer and Deserializer
If you want to register a bean as the JSON components, then you can use @JsonComponent annotation as below:
@JsonComponent
public class ModelData{
....
}
New Auto-Configuration Support
Spring boot adds new auto configuration modules for every release. In this release also there are few more modules added to support the auto-configurations.

  • Couchbase
  • Neo4j
  • Narayana Transaction Manager
  • Caffeine Cache


Actuator Improvements
There is a slight improvements on info endpoint information and metrics filter.
Now you can register the InfoContributor interface to register the beans that expose the information to the info actuator endpoints.

The support provided for:

  • Full or partial Git information generated from a build plugin
  • Build information generated from the Spring Boot Maven or Gradle plugin.
  • Custom information from the Environment (any property starting info.*)
Testing support
The biggest change comes regarding testing support. There are two new modules spring-boot-test and spring-boot-test-autoconfigure

@SpringBootTest
Spring already comes with great support for writing integration tests leveraging the spring-test module. The only problem might be that it uses lots of annotations (@SpringApplicationConfiguration, @ContextConfiguration, @IntegrationTest, @WebIntegrationTest) to accomplish it.

Spring Boot 1.4 tries to simplify this by providing a single @SpringBootTest annotation.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql("/init-for-full-integration-test.sql")
public class ApplicationTests {

 @Autowired
 private TestRestTemplate template;

 @Test
 public void random() {
  ResponseEntity<String> fact = template.getForEntity("/", String.class);

  assertThat(fact.getBody(), containsString("Chuck Norris sleeps with a pillow under his gun."));
 }
}

Here as you can see for convenience the injected TestRestTemplate is already configured to make calls against the started server, no need to inject the port with @Value("${local.server.port}"). However if you would like to know the port there is a better @LocalServerPort annotation.

Testing slices of the application
Sometimes you would like to test a simple “slice” of the application instead of auto-configuring the whole application. Spring Boot 1.4 introduces 3 new test annotations:
  • @WebMvcTest - for testing the controller layer
  • @JsonTest - for testing the JSON marshalling and unmarshalling
  • @DataJpaTest - for testing the repository layer
@WebMvcTest
In order to test only the controller layer or often a single controller the @WebMvcTest used in combination with @MockBean can be handy. @Service or @Repository components will not be scanned.
@RunWith(SpringRunner.class)
@WebMvcTest(ChuckNorrisFactController.class)
public class CheckNorrisFactControllerTests {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private ChuckNorrisFactService chuckNorrisFactService;

    @Test
    public void getOneRandomly() throws Exception {
        given(chuckNorrisFactService.getOneRandomly()).willReturn(new ChuckNorrisFact("Chuck Norris counted to infinity twice."));

        mvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().json("{'fact':'Chuck Norris counted to infinity twice.'}"));
    }
}

@JsonTest
To test JSON marshalling and unmarshalling you can use the @JsonTest annotation. @JsonTest will auto-configure Jackson ObjectMappers and @JsonComponent beans
@RunWith(SpringRunner.class)
@JsonTest
public class ChuckNorrisFactJsonTests {

    private JacksonTester json;

    @Test
    public void serialize() throws IOException {
        ChuckNorrisFact fact = new ChuckNorrisFact("When Chuck Norris turned 18, his parents moved out.");
        JsonContent write = this.json.write(fact);

        assertThat(this.json.write(fact)).isEqualToJson("expected.json");
    }

    @Test
    public void deserialize() throws IOException {
        String content = "{\"fact\":\"Chuck Norris knows Victoria's secret.\"}";

        assertThat(this.json.parse(content)).isEqualTo(new ChuckNorrisFact("Chuck Norris knows Victoria's secret."));
    }
}

@DataJpaTest

To test the JPA layer of an application you can use @DataJpaTest. By default it configures an in-memory database, scans for @Entity classes and configures the Spring Data JPA repositories. @Service, @Controller, @RestController beans will not be loaded.
@RunWith(SpringRunner.class)
@DataJpaTest
public class ChuckNorrisFactRepositoryTests {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private ChuckNorrisFactRepository repository;

    @Test
    public void getOneRandomly() {
        String fact = "If at first you don't succeed, you're not Chuck Norris.";
        entityManager.persist(new ChuckNorrisFact(fact));

        List<ChuckNorrisFact> facts = repository.getOneRandomly(new PageRequest(0, 10));
        assertThat(facts.get(0).getText(), is(fact));
    }
}


Datasource binding

In Spring Boot 1.4 via spring.datasource property namespace only the common datasource properties are set. For datasource provider specific ones new namespaces were introduced.
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.connection-timeout=10

Image Banners
You can use an image to render the ASCII banner.

Summary
If you want to try out these new features and enhancements have a look to this github repository.





No comments:

Post a Comment