<!-- swagger2 配置 -->
<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>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
package com.jinsh.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.ApiInfo;
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 Swagger2 {
// 访问地址: 官方:http://ip:port/swagger-ui.html
// 换肤:http://ip:port/doc.html
// 配置swagger2核心配置
@Bean
public Docket creatRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 用于定义api文档汇总信息
.select().apis(RequestHandlerSelectors.basePackage("com.jinsh.controller")) // 指定接口包,controller
.paths(PathSelectors.any()) // 代表所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("天天吃货 电商平台接口api") // 文档页标题
.contact(new Contact("jinsh", "https://www.jinsh.com", "abc@imooc.com")) // 联系人信息
.description("专为天天吃货提供的api文档") // 详细信息
.version("1.0.0") // 文档版本号
.termsOfServiceUrl("https://www.jinsh.com") // 网站
.build();
}
}
package com.jinsh.controller;
import com.jinsh.pojo.vo.CategoryVO;
import com.jinsh.service.CategoryService;
import com.jinsh.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(value = "首页", tags = "首页展示的相关接口")
@RestController
@RequestMapping("index")
public class IndexController {
@Autowired
CategoryService categoryService;
@ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
@GetMapping("/subCat/{rootCatId}")
public JSONResult subCat(
@ApiParam(name = "rootCatId", value = "一级分类id", required = true)
@PathVariable Integer rootCatId) {
if (rootCatId == null){
return JSONResult.errorMsg("分类不存在");
}
List<CategoryVO> list = categoryService.getSubCatList(rootCatId);
return JSONResult.ok(list);
}
}
package com.jinsh.pojo.bo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "用户对象BO", description = "从客户端由用户传入的数据封装在此类中")
public class UserBO {
@ApiModelProperty(value = "用户名", name = "username", example = "zhangsan", required = true)
private String username;
@ApiModelProperty(value = "密码", name = "password", example = "123456", required = true)
private String password;
@ApiModelProperty(value = "确认密码", name = "confirmPassword", example = "123456", required = false)
private String confirmPassword;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
}
Swagger2 Demo