Référence : https://docs.spring.io/spring-boot/docs/current/reference/html/index.html
-
initialiser un projet avec https://start.spring.io/
- maven
- java
- 2.0.0.M7
-
ajouter un service HelloService (component -> service, repository, controller) qui retourne un Greeting.
public class HelloService {
public String hello(String name) {...}
- Écrire le test
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloServiceTest {
@Autowired
private HelloService helloService;
@Test
public void simple() {
...
}
}
-
Exécuter
Run focus test method
-
Implementer CommandLineRunner
- injecter le service
@SpringBootApplication
public class App implements CommandLineRunner{
@Autowired
private HelloService helloService;
@Override
public void run(String... args) throws Exception {
System.out.println(helloService.hello("Agfa"));
}...
- Ajouter un Rest controller : https://spring.io/guides/gs/rest-service/
- Injecter le service dans le controller
RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
public Greeting hello(@RequestParam String param) {
return helloService.hello(param);
}
...
- parler du autoreload : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html
- ajouter la dépendance spring-dev-tools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
- spring-data
- ajouter la dépendance spring-boot-starter-data-jpa
- ajouter le repository de Greeting
@Repository
public interface GreetingRepository extends CrudRepository<Greeting, Long>{
List<Greeting> findByName(String name);
- ajouter l'Id dans Greeting
- ajouter la méthode HelloService.all()
- ajouter dans RestController
public List<Greeting> all(@PathVariable String name) {
return helloService.all(name);
}
-
Démo : http://localhost:8080
-
Configuration personnalisation https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
server.port=8081