因为使用的是swagger3.0
所以需要多导入一个包springfox-boot-starter
新版访问swagger路径为
http://localhost:8081/swagger-ui/index.htm
<!--导入swagger包-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
swaggerConfig配置
@Configuration
@EnableSwagger2 //开启swagger
//3.0版本默认访问路径
//http://localhost:8080/swagger-ui/index.html
public class SwaggerConfig {
@Value("${swagger.show}")
private boolean swaggerShow;
//配置bean实例
@Bean
public Docket docket()
{
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.groupName("wu")//设置组
.enable(swaggerShow)//是否开启swagger
.select()
.apis(RequestHandlerSelectors.basePackage("com.wu.controller"))
.build()
;
/*
* RequestHandlerSelectors,配置要扫描接口的方式
*.basePackage(),配置要扫描的包
*.any():扫描全部
*.none()不扫描
*.withClassAnnotation()扫描类上的注解
*.withMethodAnnotation()扫描方法上的注解
*.paths(PathSelectors.ant("/wu/**"))过滤什么路径
* */
}
//配置apiInfo 信息
public ApiInfo apiInfo()
{
//作者信息
Contact contact = new Contact("wu", "", "");
return new ApiInfo(
"Api 标题",
"Api 描述",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
application.yaml开启开发者环境
spring:
profiles:
active: dev
application-dev.yaml配置开发者环境
server:
port: 8081
swagger:
show: true
helloController配置
@RestController
public class helloController {
@GetMapping(value = "/hello")
public String hello()
{
return "hello,world";
}
@PostMapping(value = "/user")
public User user(String username,String password)
{
return new User(username,password);
}
@PostMapping(value = "/userModel")
public String userModel(String username, String password, Model model)
{
model.addAttribute("msg",new User(username,password));
return "helloModelAndView";
}