One 、 Preface

Spring Boot As the most popular Java web Development scaffold , More and more developers choose to use it to build enterprise class RESTFul API Interface . These interfaces will not only serve the traditional web End (b/s), It will also serve the mobile terminal . In the actual development process , These interfaces should also be provided for development testing to carry out relevant white box testing , Then there must be how to share and update in time in multi person cooperation API Problems in developing interface documents .

Use Swagger Integration documents have the following advantages :

  • Rich in functions  : Support multiple annotations , Automatically generate interface document interface , Support in interface testing API Interface functions ;
  • update  : Take a little time to write notes in the development process , It can be updated in time API file , Save the mind and save labour ;
  • Simple integration  : By adding pom Dependency and simple configuration , Embedded in the application can be released at the same time API Interface document interface , No need to deploy standalone Services .

Next , We go through Spring Boot To integrate Swagger Online API Functions of documents , This article USES SpringFox Swagger2, edition 2.9.2( The latest version is 3.0.0, But in my test is swagger-ui.html The page never came out , stay You can see ,2.9.2 Is currently the most used version )

Two 、 establish Spring Boot engineering

The development tool I use is IDEA, adopt IDEA Quickly create one Spring Boot engineering , Creation time , Just check web Dependent options become , It doesn't matter if you don't check it , There is pom.xml The configuration in is the same . Note that when creating a project , Project names should be in lowercase .

add to Swagger The two dependencies of

<!-- swagger -->

3、 ... and 、 Add configuration class

Add one swagger Configuration class , Build new under the project config Package and add a SwaggerConfig Configuration class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
public class SwaggerConfig { @Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
} private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title(" On-line API file ")
.description("This is a restful api document of demo")
} }

The above has been completed Swagger Configuration of , Isn't it simple and easy .

Four 、 Create a test Controller To verify the configuration

Add a controller , Build new under the project controller Package and add a HelloController controller

package com.louis.springboot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; /* Class annotation */
public class HelloController { /* Method notes */
@ApiOperation(value = "desc of method", notes = "")
public Object hello( @ApiParam(value = "desc of param" , required=true ) @RequestParam String name) {
return "Hello " + name + "!";

5、 ... and 、 Compile run test

Start the project ,  Open the browser , visit :http://localhost:8080/swagger-ui.html, Get into swagger Interface document interface .

Finish work , Manage like this API The interface is very convenient , And it is updated in real time with the code , Don't bother to write interface documents .

appendix :Swagger Commonly used annotations

API Use location
@Api be used for controller Class , Indicates that this class is swagger Resources for
@ApiOperation Use in controller On the way , It means a http Requested operation
@ApiParam Method
@ApiResponses Use in controller On the way
@ApiResponse Use in @ApiResponses inside
@ApiImplicitParams Use in controller On the way
@ApiImplicitParam Use in @ApiImplicitParams In the way of
@ApiModel For return object classes

WEB There are many strange problems in project development , You want to know how to quickly master Spring Boot, See video :

51CTO:Spring Boot+Bootstrap Development is small and complete web project

Tencent classroom :Spring Boot+Bootstrap Development is small and complete web project

CSDN college :Spring Boot+Bootstrap Development is small and complete web project

Netease cloud classroom :Spring Boot+Bootstrap Development is small and complete web project

Spring Boot Fast integration Swagger More articles about

  1. Spring Boot: Integrate Swagger file

    General overview spring-boot As the most popular Java web Development scaffold , More and more developers choose to use it to build enterprise class RESTFul API Interface . These interfaces will not only serve the traditional web End (b/s), Will also serve ...

  2. Spring Boot 2 Integrate Swagger Simple introduction

    Swagger Is a RESTFUL Documentation of the interface is automatically generated online + Functional test functional software . 1.pom.xml Add the configuration You can go to On the search springfox, You can see Sp ...

  3. Spring Boot: Integrate Spring Security

    General overview Spring Security yes Spring One of the top projects in the community , It's also Spring Boot The official recommended security framework . In addition to regular Certification (Authentication) And authorization (Author ...

  4. Spring Boot: Integrate MyBatis frame

    General overview MyBatis Is an excellent persistence layer framework , It supports customization SQL. Stored procedures and high-level mappings .MyBatis Almost all of them were avoided JDBC Code and manually set the parameters and get the result set .MyBatis It's easy to use ...

  5. Spring Boot: Integrate JdbcTemplate

    General overview Spring The operation on the database jdbc It has made a deeper encapsulation , and JdbcTemplate That is Spring Provides a convenient tool for operating the database . We can use JdbcTemplate To perform all database operations ...

  6. Spring Boot: Integrate Spring Data JPA

    General overview JPA yes Java Persistence API For short , It's a set Sun official Java Persistence specification . Its design goal is mainly to simplify the existing persistence development work and integration ORM technology , It's for Java Developers provide a way to ...

  7. Spring Boot: Integrate Shiro Permission framework

    General overview Shiro yes Apache One of its open source projects , It's a very easy to use security framework , Offers include Certification . to grant authorization . encryption . Session management and other functions , And Spring Security It's also a privilege based security framework , But with the Sprin ...

  8. Use Spring Boot Fast build Spring Framework application -- turn

    Original address : Spring Framework for a lot of Java It's no stranger to developers . since 2 ...

  9. Use Spring Boot Fast build Spring Framework application ,PropertyPlaceholderConfigurer

    Spring Framework for a lot of Java It's no stranger to developers . since 2002 Since its release ,Spring Framework has become a very popular basic framework in the field of enterprise application development . There are a lot of enterprise applications based on Spring Framework to develop .S ...

  10. Spring Boot Security Integrate JWT Realization Stateless distribution API Interface

    brief introduction JSON Web Token( abbreviation JWT) Is the most popular cross domain authentication solution .JSON Web Token Introductory tutorial - Ruan Yifeng , This article can help you understand JWT The concept of . This article focuses on Spring Boo ...

Random recommendation

  1. frequently-used HTML Code

    One . written words 1. Title Text <h#>..........</h#> #=1~6:h1 Is the largest word ,h6 Is the smallest word 2. Font changes <font>..........</fon ...

  2. Use MonkeyTest Yes Android Client progress stress testing

    Monkey yes Android A command line tool in , It can run in a simulator or in a real device . It sends a pseudo-random stream of user events to the system ( Such as key input . Touch screen input . Gesture input, etc ), Implement stress testing on the developing application . Let's start with one ...

  3. CSS Summary and sharing of confusing knowledge points - Positioning and layout

    CSS There are four modes of positioning :static.relative.absolute.fixed, Other static Is the default value , The following are the characteristics of each : static: Static positioning , In dynamic layout flow , According to the elements in the page, first ...

  4. IdentityHashMap Usage scenarios of

    IdentityHashMap Usage scenarios of JDK1.4 I joined this map type , It's using == Judge equal , instead of hashmap Of equals Method to judge equality . that , What applications does it have ? Of course, we need to ...

  5. Instruments_Automation Getting started

    Instruments Is an application used to dynamically track and analyze Mac OS X and iOS Code utility . This is a flexible and powerful tool , It allows you to track one or more processes , And check the data collected . such ,Instrume ...

  6. BZOJ 1003: [ZJOI2006] Logistics transportation trans( shortest path +dp)

    1A, Shuang ! cost[i][j] Says from the first i To the first day j The minimum cost of not changing the route every day , This can be preprocessed with the shortest path . then dp(i)=cost[j][i]+dp(j-1)+c. c For the cost of the route . --------- ...

  7. commons-logging \ log4j \ slf4j The relationship between

    The latest one web Log is used in the project , But for common logging tools ( frame ) I don't really understand , Here it is mark once . 1.commons-logging.jar common-logging yes apache One of the ...

  8. NDK common problem

    1. Ignore compilation warnings as errors APP_CPPFLAGS += -Wno-error=format-security 2. android studio Manually compile  __android_log_print wrong ...

  9. SpringMVC The next day

    SpringMVC the second day 1.   review 1.Springmvc Introduce ? Springmvc yes Spring company 2.Springmvc Entry procedure First step :Web engineering The second step : guide Jar package The third step :web.x ...

  10. MySQL The relationship between tables

    The relationship between tables surface 1 foreign key surface 2 Schedule 1 The corresponding table of multiple records 2 A record of , Many to one utilize foreign key We can make two tables of many to many , One to one relationship Many to many : surface 1 Multiple records of can ...