Skip to content

Configure Swagger

Imad laghrissi edited this page Jan 1, 2020 · 1 revision

Welcome to the TutorialHibernatePostgres wiki

Swagger

  • Document API
  • Interact with API : Test web service without install other tools like postman, soapUI,...

Step 1 : add swagger dependency

	<!-- swagger dependency -->
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.9.2</version>
	</dependency>
	<!-- swagger dependency to generate html documentation -->
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.9.2</version>
	</dependency>

Step 2 : enable swagger

add annotation @EnableSwagger2 of package springfox.documentation.swagger2.annotations in the main class in order to test if it works : http://localhost:8083/v2/api-docs it should return json which describe web services

Step 3 : add documentation:

For web services:

	@ApiOperation(value = "View a list of available employee", response = Iterable.class)
	@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved list"),
			@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
			@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
			@ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })

For dto objects:

@ApiModelProperty(notes = "employee id") 

Step 4 : View documentation UI

http://localhost:8083/swagger-ui.html