Skip to content

Instantly share code, notes, and snippets.

@lfo
Last active January 19, 2018 11:42
Show Gist options
  • Save lfo/d68c2fdbc97270b62c5e79a03158648e to your computer and use it in GitHub Desktop.
Save lfo/d68c2fdbc97270b62c5e79a03158648e to your computer and use it in GitHub Desktop.

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