SpringBoot
Introduction
- Framework to build microservices
- common non-functional features
- not zero code generation
- not a web or application server
- provides starter projects
- embedded server (incl. in application jar)
- externalized configuration
Concepts
- SpringBoot Autoconfiguration: by scanning the classpath
- Dispatcher Servlet
- SpringBoot Actuator (for monitoring)
Security
adding to pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
and update the project automatically sets up form-based authentication with a generated session cookie generated on the server or basic authentication with a header sent along with every request
- user='user
- pwd=see console output of server start
or set it in application.properties
spring.security.user.name=uwe spring.security.user.password=uwe
This has to be entered just once.
Testing
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
This integrates the frameworks JUnit, Hamcrest, Mockito,
Configuration
Application Properties
in \<Project>\src\main\resources\application.properties
logging.level.org.springframework = debug #security configuration see here #H2 database configuration see here
Implementation
Main Application
see /test1/src/main/java/com/uweheuer/springboot/test1/Test1Application.java/
@SpringBootApplication
public class Test1Application {
...
public static void main(String[] args) {
SpringApplication.run(Test1Application.class, args);
}
...
@SpringBootApplication
is a convience annotation that adds @Configuration, @EnableAutoConfiguration, @EnableWebMvc and @ComponentScan.
Rest Controller
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @CrossOrigin(origins="http://localhost:4200") // avoid cross origin errors in browser public class HelloWorldController { // @RequestMapping(method=RequestMethod.GET, path="/hello-world") @GetMapping(path="/hello-world")
@RequestMapping
@RequestMapping("/") // by default it maps HTTP operations
Integration Test
@SpringBootTest
class BookmarksApplicationTests {
@Test
void contextLoads() {
}
}
Resources
Examples
Udemy Course
Bookmarks
- start eclipse
- File -> Import -> Maven -> Existing Maven Projects
- select
C:\Uwes\eclipse\workspace_2020-12\SpringBoot\bookmarks
Database Configuration
The default is that the datasource url is generated randomly and printed to the console. The default user is 'sa', default password is empty.
spring.profiles.active=[dev|laptopmysql|raspberry]
- and created in
\bookmarks\src\main\resources\dedicated property filesapplication-[dev|laptopmysql|raspberry].properties
ToDos
- adjust maven build
- build entities
- https://gist.github.com/michail-nikolaev/3840973
- https://medium.com/@kthsingh.ms/modeling-a-child-parent-relationship-in-the-same-table-using-jpa-spring-boot-and-representing-it-15e5a6256dab
- https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
- https://stackoverflow.com/questions/2749689/what-is-the-owning-side-in-an-orm-mapping
- https://www.javacodegeeks.com/2013/04/jpa-determining-the-owning-side-of-a-relationship.html
- The @ManyToOne association uses FetchType.LAZY because, otherwise, we’d fall back to EAGER fetching which is bad for performance.
- https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/