Vous êtes sur la page 1sur 6

Etablissement : Tekup Niveau : GLSI 2

Matière : Web services Année Universitaire : 2022 - 2023

TP n°4- Swagger2

Objectif
Description du service développé dans le TP3 avec Swagger2.

1. Modification du pom.xml : Modifier le fichier pom.xml du projet tp3 en remplaçant la section


« dependencies » par la partie suivante

<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>

2. Configuration du swagger2 : Ajouter une classe nommée Swagger2UiConfiguration dans le


package com.tekup.tp3.configuration avec le code suivant

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

public class Swagger2UiConfiguration extends


WebMvcConfigurerAdapter {

@Bean

public Docket api() {

// @formatter:off

//Register the controllers to swagger


//Also it is configuring the Swagger Docket

return new Docket(DocumentationType.SWAGGER_2).select()

.apis(RequestHandlerSelectors.any())

//
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.spri
ngframework.boot")))

.paths(PathSelectors.any())

//.paths(PathSelectors.ant("/swagger2-demo"))

.build();

// @formatter:on

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry)


{

// enabling swagger-ui part for visual documentation

registry.addResourceHandler("swagger-
ui.html").addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**").addResourceLocations("c
lasspath:/META-INF/resources/webjars/");

3. Faire une mise à jour Maven du projet


4. Redémarrer le projet
5. Accéder aux descriptions OpenAPI
a. http://localhost:8080/v3/api-docs
b. http://localhost:8080/swagger-ui/
6. Personalisation de la description OpenAPI :Modifier la classe Student en ajoutant les
annotations swagger

......

.....
@ApiModelProperty(notes = "Name of the Student", name = "name",
required = true)

private String name;

@ApiModelProperty(notes = "Standard of the Student", name =


"standard", required = true)

private int standard;

@ApiModelProperty(notes = "Address of the Student", name =


"address", required = true)

private String address;

....

....

7. Personalisation de la description OpenAPI : Modifier la classe StudentController en


ajoutant les annotations swagger

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;

@Tag(name = "StudentController", description = "the Student API


with some description")

@RestController

@RequestMapping("students")

public class StudentController {

@Autowired

private StudentRepository repository;

@Operation(summary = "Get list of Students in the System ", tags =


"getStudents")

@ApiResponses(value = {

@ApiResponse(responseCode = "200", description = "Success|OK"


,content =

{ @Content(mediaType = "application/json", schema =

@Schema(implementation = Student.class)) }),

@ApiResponse(responseCode = "401", description = "not


authorized!"),

@ApiResponse(responseCode = "403", description = "forbidden!!!"),

@ApiResponse(responseCode = "404", description = "not found!!!")


})

@GetMapping()

ResponseEntity<List<Student>> all() {

return ResponseEntity.ok(repository.selectAll());

@ApiIgnore
@Operation(summary = "Add a new Student to the System ", tags =
"postStudent")

@PostMapping()

ResponseEntity<Boolean> newStudent(@RequestBody Student


newStudent) {

return ResponseEntity.ok(repository.addStudent(newStudent));

@Operation(summary = "Get Student By Name", description = "Student


must exist", tags = "getStudent")

@GetMapping("/{name}")

ResponseEntity<Student> one(@PathVariable String name) {

return ResponseEntity.ok(repository.recherche(name));

@Operation(summary = "Delete a specific Student from the System ",


description = "Student must exist", tags = "deleteStudent", hidden
= true)

@DeleteMapping("/{name}")

void deleteStudent(@PathVariable String name) {

repository.supprimer(name);

8. Accéder aux descriptions OpenAPI


a. http://localhost:8080/v3/api-docs
b. http://localhost:8080/swagger-ui/

Vous aimerez peut-être aussi