Académique Documents
Professionnel Documents
Culture Documents
TP n°4- Swagger2
Objectif
Description du service développé dans le TP3 avec Swagger2.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
package com.tekup.tp3.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerR
egistry;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
Adapter;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import
springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@Bean
// @formatter:off
.apis(RequestHandlerSelectors.any())
//
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.spri
ngframework.boot")))
.paths(PathSelectors.any())
//.paths(PathSelectors.ant("/swagger2-demo"))
.build();
// @formatter:on
@Override
registry.addResourceHandler("swagger-
ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("c
lasspath:/META-INF/resources/webjars/");
......
.....
@ApiModelProperty(notes = "Name of the Student", name = "name",
required = true)
....
....
package com.tekup.tp3.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tekup.tp3.model.Student;
import com.tekup.tp3.repository.StudentRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import springfox.documentation.annotations.ApiIgnore;
@RestController
@RequestMapping("students")
@Autowired
@ApiResponses(value = {
@GetMapping()
ResponseEntity<List<Student>> all() {
return ResponseEntity.ok(repository.selectAll());
@ApiIgnore
@Operation(summary = "Add a new Student to the System ", tags =
"postStudent")
@PostMapping()
return ResponseEntity.ok(repository.addStudent(newStudent));
@GetMapping("/{name}")
return ResponseEntity.ok(repository.recherche(name));
@DeleteMapping("/{name}")
repository.supprimer(name);