Using swagger2 in SpringMVC

Swagger is an API document generation tool. When writing commercial projects, each project may need to call the API interface of other projects, such as front-end and back-end separation projects. In this case , API documents need to be written. Documents, programmers hate themselves the most . Write something that others don't write, and Swagger can automatically generate documents based on the annotations configured in the project, then start adding this little thing

Introduce jar package

 <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
    </dependency>

Add configuration

Create SwaggerConfig configuration class

@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {

	/**
	 * 通过createRestApi函数创建Docket的Bean之后,
	 * apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
	 * select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,
	 * apis()函数扫描所有Controller中定义的API, 并产生文档内容(除了被@ApiIgnore指定的请求)
	 * @return
	 */
	@Bean
	public Docket createRestApi() {

		return new Docket(DocumentationType.SWAGGER_2)
//				.globalOperationParameters(operationParameters)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any())
				.build();
		
	}

	/**
	 * 创建该Api的基本信息(这些基本信息会展现在文档页面中)
	 * @return
	 */
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("标题")
				.termsOfServiceUrl("http://www.itrip.com/auth")
				.contact("[email protected]")
				.version("1.0")
				.build();
	}
}

Pay attention to adding in the springMVC configuration file

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />

Avoid being intercepted

use

Use annotations to declare @Api on the controller class
(tags = “Class”) =》 Declare class
@ApiOperation(value = “Interface name”, notes = “Interface description”, httpMethod = “Sending method”) =》 Declare interface
@ ApiParam(value = "Parameter name", name = "Parameter name") => Declare parameters

Example:

@Controller
@Api(tags = "UserController")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "接口名",notes = "接口描述",httpMethod = "post")
    @RequestMapping(value="login",method= RequestMethod.POST)
    @ResponseBody
    public Dto login(
			@RequestParam("userCode")
			@ApiParam(value = "参数说明",name = "参数名") String userCode){

        ItripUser user = userInfoService.getUserInfo(userCode);

        return DtoUtil.returnDataSuccess(user);
    }
}

Note that when declaring a method, it is best to specify the current access method for interface access, which is the method attribute in @RequestMapping. Otherwise, all request methods will be displayed in the swagger document, which is very messy. It is also best to add @RequestParam. Otherwise, the test function on swagger may not be available

After everything is done, restart the project and visit: ** project address/swagger-ui.html **

Common exception: 404 reported when accessing the page

1. Check whether springMVC static resources are configured

2. Check whether the path is correct

Guess you like

Origin blog.csdn.net/lihao1107156171/article/details/103014234