<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.7.0version>
dependency>
2.Swagger配置
package com.gary.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.globalOperationParameters(globalOperationParameters())// 全局参数
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/.*"))// 监控所有路径
.build();
}
private List
ParameterBuilder builder = new ParameterBuilder();
Parameter parameter = builder.name("token").description("登录接口返回的token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false).build();
ArrayList
parameters.add(parameter);
return parameters;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试API文档")
.description("格伦发展有限公司 Copyright 2000-2030 (C) All Rights Reserved.")
.termsOfServiceUrl("http://www.gary.cn/")
.contact(new Contact("gary",null,"[email protected]"))
.version("1.0")
.build();
}
}
3.Controller类添加说明
package com.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
@Api(description = "登录/注销接口")
public class ThemeleafController {
@ApiOperation(value = "跳转登录页面")
@GetMapping("/toLogin")
public String toLogin(){
return "user/login";
}
@ApiOperation(value = "查询用户接口")
@ApiImplicitParams({ @ApiImplicitParam(name = "address", value = "地址", paramType = "query", required = true),
@ApiImplicitParam(name = "age", value = "年龄", paramType = "query", required = true)})
@RequestMapping(value="/users",method = RequestMethod.GET)
public Result.Base login(HttpServletRequest request, String address, String age){
User user = User.builder().build();
return new Result.Success<>(user);
}
@PostMapping("/doLogin")
@ApiOperation(value = "登录接口")
public String doLogin(@RequestBody LoginVo loginVo){
System.out.println(loginVo.toString());
return "home";
}
}
实体类中:
@Data
@Builder
@ApiModel(description = "登录参数")
public class LoginVo {
@ApiModelProperty(value = "用户名", required = false)
private String username;
@ApiModelProperty(value = "密码", required = false)
private String password;
}
4.访问
http://localhost:8087/ swagger-ui.html
5.问题
访问页面时找不到页面,后台报:
No mapping for GET /swagger-ui.html
说明Spring Boot自动配置本身没有把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。添加配置类:
package com.wps.education.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
或者在拦截器中直接添加:
package com.gary.interseptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableWebMvc
@EnableSwagger2
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter{
/**
* 拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/toLogin","/doLogin",
"/swagger-resources/**", "/v2/**", "/static/**","/error/**",
"/webjars/**", "/swagger-ui.html/**","/swagger-resources/**");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}