- Overview
- Features
- Starting
- Dependencies
- Running
- Annotations
- Entities
- Repositories
- Controllers
- Interfaces
A quick and easy way to get a Spring project up and running. By following Spring conventions and using certain third-party applications, one can have a Spring project up in a few minutes.
- Stand-alone Spring applications
- Uses embedded servlet (Tomcat default) so no need to deploy a WAR
- Provides "starter poms" to bring in all dependencies and versions
- No code generation or XML configuration
- start.spring.io
- Easy website to start a new Spring Boot application
- Select metadata items
- POM information
- Group
- Artifact
- Name
- Description
- Java version
- Spring Boot version
- Packaging
- POM information
- Select starter dependencies
- Core
- Web
- Template Engines
- Data
- Cloud
- Database
- Social
- I/O
- Ops
For this demo, I have selected the following dependencies:
- Data
- JPA
- JDBC
- Database
- H2
- Web
- Web
- Rest Repositories
This application can be ran in multiple ways:
- From Eclipse as a Java Application
- From Maven via
mvn spring-boot:run
- From a packaged JAR
java -jar spring-boot-0.0.1-SNAPSHOT.jar
Since this is just a JAR, this project has a main class, DemoApplication. For convenience, there is a static method that can be invoked to start the whole application:
SpringApplication.run(DemoApplication.class, args);
Where DemoApplication
is the name of the main class in my project and args are the same arguments passed into main
.
Several annotations are used throughout this project:
@SpringBootApplication
- This is equivalent to using@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
with their default attributes. This sets up the Spring application to look for all other annotations, scan for beans, and autowire everything.@Component
- Indicates a class should be picked up by auto-detection for running.@Order
- Indicates the order in which a specific component should be ran.@RestController
- This is equivalent to using@Controller
and@ResponseBody
. It indicates a class is a controller for REST requests. It also implicitly makes each method return a JSON response body.@Autowired
- Autowires in objects/dependencies.@RequestMapping
- Indicates a REST endpoint inside of a controller.@ResponseBody
- Indicates that the response body should be JSON.@Param
- Indicates a REST request parameter.@Entity
- Indicates an entity which should be stored in a database. With no other config, the database table is the name of the class.@Id
- Indicates which field is the primary id on an entity.@GeneratedValue
- Indicates that the id field should be an auto-generated number.
- Customer - Compromised of a first and last name.
- Person - Compromised of a first and last name.
- CustomerRepository - A
CrudRepository
with extra search methods. - PersonRepository - A
PagingAndSortingRepository
with extra search methods.
- CustomerController - A REST controller built on top of the
CustomerRepository
. Provides simple REST endpoints with request parameters and returns results in JSON. - PersonRepository - A very simple REST controller built in the repository itself. Provides searches based on the interface methods, and sorting/pagination based on interface it extends.
- Banner - Used to create a custom banner on startup.
- CommandLineRunner - Used to run code immediately after the full Spring environment has been set up.
- CrudRepository - Basic repository interface that provides simple CRUD operation methods:
- Save
- One
- Multiple
- Find
- One by id
- Multiple by id
- All
- Exists by id
- Count
- Delete
- One by id
- One by entity
- Multiple by entity
- All
- PagingAndSortingRepository - Repository built on top of the
CrudRepository
. Adds pagination and sorting operations: - Find all
- By some sorting
- By pagination