Skip to content

Instantly share code, notes, and snippets.

@lfo
Last active Jan 19, 2018
Embed
What would you like to do?

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"));
    }...
RestController
public class HelloController {
    
    @Autowired
    private HelloService helloService;
    
    @RequestMapping("/hello")
    public Greeting hello(@RequestParam String param) {
        return helloService.hello(param);
    }
    ...
<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);
    }
server.port=8081
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment