This post walks you through the process of creating a simple registration and login example with Spring Security using database. Please read this previous post before conitnue with this information. First we need to add Spring Data JPA dependency to the build.gradle
compile 'org.springframework.boot:spring-boot-starter-data-jpa' Then we need to change Spring security Java config class in order to add userDetailsService to implement database access functionality:
package com.jos.dem.springboot.security.config import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Configuration import org.
[Read More]
Spring Boot Security
Spring Security is a powerful and highly customizable authentication and access-control framework. This technical post will review how to integrate security into your Spring Boot project. NOTE: If you need to know what tools you need to have installed on your computer to create a Spring Boot basic project, please refer to my previous post: Spring Boot. Then, let’s create a new Spring Boot project with web, security and Lombok as dependencies:
[Read More]
Spring Boot Testing Validation
This time I will show you how create a unit testing using Spock, Spock is a testing and specification framework for Java and Groovy applications it is highly expressive and easy to use. First you need to add Spock dependency in your build.gradle
testCompile 'org.spockframework:spock-spring' This is the class we are going to test:
package com.jos.dem.vetlog.validator import org.springframework.validation.Validator import org.springframework.validation.Errors import org.springframework.stereotype.Component import org.springframework.beans.factory.annotation.Autowired import com.jos.dem.vetlog.service.LocaleService import com.jos.dem.vetlog.command.UserCommand @Component class UserValidator implements Validator { @Autowired LocaleService localeService @Override boolean supports(Class<?
[Read More]
Spring Boot Validation
Spring has a Validation interface that we can use to create custom object validation; if some errors occur in the validation process, it stores them in a BindingResult object so you can test for and retrieve validation errors. Let’s consider the following validator implementation:
package com.josdem.springboot.validation.validator; import com.josdem.springboot.validation.command.PersonCommand; import org.springframework.validation.Validator; import org.springframework.validation.Errors; import org.springframework.stereotype.Component; @Component public class PersonValidator implements Validator { private final String REGEX = "[0-9]+"; @Override public boolean supports(Class<?
[Read More]
Spring Boot Bootstrap
Boostrap was originally created by a designer and a developer at Twitter, Bootstrap has become one of the most popular front-end frameworks and open source projects in the world. In this technical post I will show you how to add Bootstrap to your Spring Boot project. In order to get the setup for this example, please refer my previous posts Spring Boot JPA and Spring Boot Externalization. First you need to add Bootstrap dependency to your project, the way you should do it is add it as a Bower plugin to your build.
[Read More]
Spring Boot Testing
We can test our application using Junit or Spock, and we will see both. Also you can make unit, integration and functional testing.
Integration testing Spring Boot provides a @SpringApplicationConfiguration annotation as an alternative to the standard @ContextConfiguration annotation.
package com.jos.dem.jmailer import static org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.springframework.boot.test.SpringApplicationConfiguration import org.springframework.test.context.junit4.SpringJUnit4ClassRunner import org.springframework.beans.factory.annotation.Autowired import org.springframework.mail.javamail.JavaMailSenderImpl @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = JmailerApplication.class) class JmailerApplicationTests { @Autowired JavaMailSenderImpl javaMailSender @Test void shouldLoadContext() { assertEquals('smtp.
[Read More]
Spring Boot Externalization
In this example we are going to externalize MySQL database connection using a yaml file. In order to get the setup for this example, please refer my previous post Spring Boot JPA
First we need to create our yml file in ${USER_HOME}/.spring-boot-jpa/application-development.yaml
spring: datasource: url: jdbc:mysql://localhost/spring_boot_jpa username: username password: password driverClassName: com.mysql.jdbc.Driver jpa: generateDdl: true Next we need to add bootRun closure and specify a system properties in our build.gradle file
[Read More]
Spring Boot JPA
The key goal of Spring Data repository abstraction is to significantly reduce the amount of code required to implement data access layers for various persistence stores. In this example I will show you how to create a JPA repository to save and retrieve all Person model. NOTE: If you need to know what tools you need to have installed in yout computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot
[Read More]
Spring Boot Internationalization
In this technical post, we will see how to manage different languages at your Spring Boot application aka. internationalization. NOTE: If you need to know what tools you need to have installed in your computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot. Spring Boot uses MessageSource configured with a MessageSourceAutoConfiguration. These settings can be easily changed in the application.properties file:
spring.messages.basename=i18n/messages Once we create that line, you can define your application messages in the file resources/i18n/messages.
[Read More]
Spring Boot Handler Exception
There are several reasons why we should manage exceptions such as:
Customize an error page Business logic Reduce logic The common way to manage exceptions in Spring is using an HandlerExceptionResolver class, in this example I will show you how to manage an generic exception in our code.
package com.jos.dem.jmailer.controller import org.springframework.web.servlet.HandlerExceptionResolver import org.springframework.http.HttpStatus import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse import org.springframework.stereotype.Component import org.springframework.http.ResponseEntity import org.springframework.web.servlet.ModelAndView import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory @Component class HandlerException implements HandlerExceptionResolver { Log log = LogFactory.
[Read More]