Spring Framework
“Spring Boot project is just a regular Spring project that happens to leverage Spring Boot starters and auto-configuration”
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The @SpringBootApplication enables Spring component-scanning and Spring Boot auto-configuration
@SpringBootApplication combines three other useful annotations @Configuration, @ComponentScan, @EnableAutoConfiguration
$ gradle bootRun
The bootRun task comes from Spring Boot’s Gradle plugin. Alternatively, you can build the project with Gradle and run it with java at the command line
$ gradle build
...
$ java -jar build/libs/firstApp-0.0.1-SNAPSHOT.jar
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'SampleSpringBoot'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Defines the class as a Spring Restful controller
@RestController
public class HelloWorldController {
}
Define URL mapping for a class and/or method
@RestController
public class HelloWorldController {
@RequestMapping(value = "/helloWorld", method = { RequestMethod.GET })
public String helloWorld() {
return "helloWorld";
}
}
Arguments and return types
@RestController
public class SomeController {
@RequestMapping(value = "/pets", method=RequestMethod.POST)
public Pet addPet(@RequestBody Pet pet) {
// implementation omitted
return someObj;
}
}
In Spring MVC use the @PathVariable annotation on a method argument to bind it to the value of a URI template variable
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
// implementation omitted
}
or
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner) {
// implementation omitted
}
A method can have any number of @PathVariable annotations
@RequestMapping(value="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId) {
// implementation omitted
}
eg.
http://www.example.com/owners/42/pets/21
Use the @RequestParam annotation to bind request parameters to a method parameter in controller
@RestController
@RequestMapping("/pets")
public class PetController{
@RequestMapping(method = RequestMethod.GET)
public Pet setupForm(@RequestParam("petId") int petId) {
Pet pet = this.clinic.loadPet(petId);
return pet;
}
}
***Parameters using this annotation are required by default
e.g., @RequestParam(value="id", required=false)
Indicates that a method parameter should be bound to the value of the HTTP request body
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body {
// implementation omitted
}
Indicates that the return type should be written straight to the HTTP response body
@RequestMapping(value = "/something", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return "Hello World";
}
“H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode.”
dependencies {
runtime('com.h2database:h2')
}
Add configuration in application.properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:~/springboot;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.database-platform=H2
Update configuration AUTO_SERVER=TRUE
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:~/springboot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE;
spring.jpa.database-platform=H2
update application.properties
spring.datasource.platform:h2
create file data-h2.sql in /src/main/resources/data-h2.sql
insert into employee (first_name, last_name) values ('john', 'doe');
insert into employee (first_name, last_name) values ('james', 'doe');
insert into employee (first_name, last_name) values ('smith', 'doe');
insert into employee (first_name, last_name) values ('Assanai', 'Manurat');
enable configuration to enable spring boot execute script
spring.datasource.initialize=true
@Entity
public class Employee {
@Id
@GeneratedValue
private Integer id;
private String firstName;
private String lastName;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
}
@Repository
@Transactional(readOnly = true)
public class EmployeeRepository {
@PersistenceContext private EntityManager entityManager;
public List<Employee> findAll() {
Query query = entityManager.createQuery("from Employee");
return query.getResultList();
}
public Employee findById(Integer id) {
return entityManager.find(Employee.class, id);
}
}
public Employee findByLastName(String lastName) {
Query query = entityManager.createQuery("from Employee e where e.lastName = :LAST_NAME");
query.setParameter("LAST_NAME", lastName);
List resultList = query.getResultList();
return resultList.isEmpty()? null : (Employee) resultList.get(0);
}
public List<Employee> findAllByNativeQuery() {
Query nativeQuery = entityManager.createNativeQuery("select id, first_name, last_name from employee", Employee.class);
return nativeQuery.getResultList();
}
public interface EmployeeCrudRepository extends JpaRepository<Employee, Integer> {
}
public interface EmployeeCrudRepository extends JpaRepository<Employee, Integer> {
List<Employee> findByFirstName(String firstName);
List<Employee> findByFirstNameAndLastName(String firstName, String lastName);
}
Create file data.sql in classpath
insert into employee (first_name, last_name) values ('john', 'doe');
insert into employee (first_name, last_name) values ('james', 'doe');
insert into employee (first_name, last_name) values ('smith', 'doe');
insert into employee (first_name, last_name) values ('Assanai', 'Manurat');
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
@RunWith(SpringRunner.class)
public class EmployeeControllerMockitoUnitTest {
private MockMvc mockMvc;
@InjectMocks
private EmployeeController controller = new EmployeeController();
@Mock
private EmployeeRepository employeeRepository;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
// this.controller.setEmployeeRepository(employeeRepository);
this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void getEmployeesShouldReturnListOfEmployees() throws Exception {
List<Employee> employees = new ArrayList<>();
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("john");
employee.setLastName("doe");
employees.add(employee);
BDDMockito.given(employeeRepository.findAll()).willReturn(employees);
this.mockMvc.perform(MockMvcRequestBuilders.get("/employees"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.jsonPath("$", hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].id", equalTo(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].firstName", equalTo("john")))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].lastName", equalTo("doe")))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
@RunWith(SpringRunner.class)
@WebMvcTest(EmployeeController.class)
public class EmployeeControllerUnitTest {
@Autowired private MockMvc mockMvc;
@MockBean private EmployeeRepository employeeRepository;
@Test
public void testListEmployeeShouldReturnListOfEmployees() throws Exception {
List<Employee> employees = new ArrayList<>();
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("john");
employee.setLastName("doe");
employees.add(employee);
BDDMockito.given(employeeRepository.findAll()).willReturn(employees);
this.mockMvc.perform(MockMvcRequestBuilders.get("/employees"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].id", Matchers.equalTo(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].firstName", Matchers.equalTo("john")))
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].lastName", Matchers.equalTo("doe")))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
@Profile("dev")
@Service
public class HelloService {
public String sayHello() {
System.out.println("Hello, develop profile");
}
}
@Profile("production")
@Service
public class HelloService {
public String sayHello() {
System.out.println("Hello, production profile");
}
}
Add configuration in application.properties
spring.profiles.active=dev
$ java -jar SampleSpringBoot-1.0-SNAPSHOT.jar
--server.port=8000 --spring.profiles.active=dev