Here is a Spring Boot cheatsheet:
Spring Boot Configuration
Quickstarts
- Cloud: https://start.spring.io/
- CLI: https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
Parent Pom (latest version):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.3</version> <relativePath/> </parent>
Gradle
plugins { id 'org.springframework.boot' version '3.5.3' }
Basic Project creation with spring init:
spring init -dweb,jpa
Common starters:
- web Web applications using Spring MVC (Tomcat embedded)
- test Spring test using JUnit, Hamcrest and Mockito
- security Secured services with Spring Security
- webflux WebFlux applications using Spring Framework’s Reactive Web
- websocket WebSocket applications using Spring Framework’s WebSocket
- data-jdbc Configured resources to use Spring Data JDBC
- data-jpa Persist data in SQL stores with Java Persistence API
- data-rest Web applications using Spring Data repositories over REST
- actuator Production ready features using Spring’s Actuator (monitor and manage)
Spring Boot Coding
Base class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
RestController example (RequestMapping)
@RestController
public class CustomerController {
@RequestMapping("/") public List <Customer> findAll() {
List < Customer > customerList = new ArrayList < Customer > ();
customerList.add(new Customer(1, "frank"));
customerList.add(new Customer(2, "john"));
return customerList;
}
}
RestController (GetMapping & PostMapping)
@RestController
public class CustomerController {
@Autowired
CustomerRepository repository;
@GetMapping("/list")
public List<Customer> findAll() {
return repository.getData();
}
@GetMapping("/one/{id}")
public Customer findOne(@PathVariable int id) {
return repository.getData().get(id);
}
@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Customer> create(@RequestBody Customer customer) {
repository.saveCustomer(customer);
return new ResponseEntity<>(customer, HttpStatus.CREATED);
}
}
Test Class example (RestAssured):
package com.example.testrest;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.get;
import static org.hamcrest.CoreMatchers.is;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {
@Test
public void testCustomerList() {
get("http://localhost:8080/list").then().assertThat().statusCode(200).body("size()", is(2));
get("http://localhost:8080/one/0")
.then()
.assertThat()
.statusCode(200)
.body("name", Matchers.equalTo("frank"));
get("http://localhost:8080/one/1")
.then()
.assertThat()
.statusCode(200)
.body("name", Matchers.equalTo("john"));
}
}
Found the article helpful? if so please follow us on Socials