Swagger整合SpringBoot
引入的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Configuration类
package cn.liuweiwei.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/").select()
.apis(RequestHandlerSelectors.basePackage("cn.liuweiwei.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("Springboot整合Swagger")
.description("Springboot整合swagger详细信息....")
.version("版本号1.0")
.contact(new Contact("vivi","http://www.vivi.cn","2500247684@qq.com"))
.license("The Vivi License")
.licenseUrl("http://www.vivi.cn")
.build());
}
}
浏览器地址栏访问的路径
http://localhost:8080/swagger-ui.html
Swagger相关注解
@Api(tags="")
@ApiOperation(value="",notes="")
@ApiImplicitParams(name="",value="",dataType="","paramType="")
@ApiResponses({@ApiResponse(code=404,message="")})
package cn.liuweiwei.controller;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/user")
@Api(tags = "用户服务相关接口的描述")
public class HelloController {
@GetMapping("/findAll")
@ResponseBody
@ApiOperation(value = "用来查询所有用户的信息",notes = "返回所有用户的数据")
@ApiResponses({//设置响应的状态码的相关信息
@ApiResponse(code = 404,message = "访问的资源不存在"),
@ApiResponse(code = 500,message = "服务器繁忙...")
})
public Map<String,Object> findAll(){
HashMap<String, Object> map = new HashMap<>();
map.put("success",true);
map.put("noSuccess",false);
return map;
}
@PostMapping("/save/{username}/{password}")
@ResponseBody
@ApiOperation(value = "保存一个用户的接口",notes = "一次只能保存一个用户")
@ApiImplicitParams({ //对接口的参数进行详细说明
@ApiImplicitParam(name = "username",value = "用户名",dataType = "String",
defaultValue = "vivi",paramType = "path"),
@ApiImplicitParam(name = "password",value = "用户的密码",dataType = "String",
defaultValue = "123",paramType = "path")
})
public String save(@PathVariable("username") String username,
@PathVariable("password") String password){
System.out.println(username+"==" + password);
return "保存成功!!";
}
}