(1)pom添加
<!--引入swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.3.1</version> </dependency>
注意版本号,2.3.1是可以。 2.6或者2.7好像跑不起来,注解写法有问题
(2)springMvc的xml里,添加
<mvc:resources mapping="/swagger-ui.html" location="classpath:/META-INF/resources/" /> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" /> <bean class="cn.xxxx.index.controller.SwaggerConfig" id="swagger2Config"/>
如果换高版本的话,location的路径好像也不一样,坑爹 swagger要用springMVC的context,所以要放在mvc的xml里,别放spring的xml里。
(3)写一个配置类
package cn.xxx.controller;
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.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @EnableSwagger2 //启用Swagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .pathMapping("/") .select() .paths(PathSelectors.regex("/.*")) .build(); }
private ApiInfo apiInfo() { return new ApiInfoBuilder().title("xxx接口文档") .contact("aaa@aa.com") .description("这是swaggerUI生成的接口文档") .version("1.0.0.0") .build(); } }
(4)在你的controller类上写一堆注解 @ApiOperation不写httpMethod的话,会自动生成6种,烦死 @ApiResponse的code就是http的状态码,坑爹。瞎写会报错,No matching constant for 123什么的。
package cn.xxx.index.controller;
import io.swagger.annotations.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List;
/** */ @RestController @Api(value = "电影Controller", tags = { "电影访问接口" }) @RequestMapping("/film") public class FilmController {
/** * 添加一个电影数据 * * @param * @return */ @ApiOperation(value = "添加一部电影",httpMethod = "POST") @RequestMapping("/addFilm") @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"), @ApiResponse(code = 502, response = Integer.class,message = "缺少参数") }) public int addFilm(@ApiParam("电影名称") @RequestParam("filmName") String filmName, @ApiParam(value = "分数") @RequestParam("score") Short score, @ApiParam("发布时间") @RequestParam(value = "publishTime",required = false) String publishTime, @ApiParam("创建者id") @RequestParam("creatorId") Long creatorId) {
return 1; }
/** * 根据电影名字获取电影 * * @param fileName * @return */ @RequestMapping("/getFilms") @ApiOperation(value = "根据名字获取电影",httpMethod = "GET") @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"), @ApiResponse(code = 502, message = "缺少参数") }) public List<String> getFilmsByName(@ApiParam("电影名称") @RequestParam("fileName") String fileName) { List<String> films = new ArrayList<>(); return films;
}
/** * 根据电影名更新 * * @return */ @RequestMapping("/updateScore") @ApiOperation(value = "根据电影名修改分数",httpMethod = "POST") @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"), @ApiResponse(code = 502, message = "缺少参数") }) public int updateFilmScore(@ApiParam("电影名称") @RequestParam("fileName") String fileName, @ApiParam("分数") @RequestParam("score") Short score) { return 1; }
/** * 根据电影名删除电影 * * @param request * @return */ @RequestMapping("/delFilm") @ApiOperation(value = "根据电影名删除电影",httpMethod = "POST") @ApiImplicitParams({ @ApiImplicitParam(name = "filmName", value = "电影名", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "id", value = "电影id", dataType = "int", paramType = "query") }) public int deleteFilmByNameOrId(HttpServletRequest request) { return 1; }
/** * 根据id获取电影 * * @param id * @return */ @RequestMapping("/{id}") @ApiOperation(value ="根据id获取电影",httpMethod = "GET") @ApiImplicitParam(name = "id", value = "电影id", dataType = "long", paramType = "path", required = true) public String getFilmById(@PathVariable Long id) { return "aaa"; }
/** * 修改整个电影 * * @param film * @return */ @RequestMapping("/insertFilm") @ApiOperation(value ="插入一部电影",httpMethod = "POST") public int insertFilm(@ApiParam("电影实体对象") @RequestBody String film) { return 1; } }
(5) 高版本(如2.7或3.0版本)的ApiResourceController类, @RequestMapping({"/swagger-resources"}) @ResponseBody ResponseEntity<List<SwaggerResource>> swaggerResources() { 这个@RequestMapping注解是写在类上的,方法上没有,导致不认,会404,折腾好久才发现,所以降到了2.3.1版本,就正常了。 坑爹
(6)增加Knife4j的话,就是换一个皮肤 <1>pom加引用
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
<2>springMvc的xml加 <mvc:resources mapping="doc.html" location="classpath:/META-INF/resources/"/> <3>用http://ip:port/xxx/doc.html访问
|