在pom文件中添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
修改spring-mvc.xml文件配置
<!--将静态资源交由默认的servlet处理-->
<mvc:default-servlet-handler />
<!--向容器自动注入配置-->
<context:annotation-config />
<!--自动扫描,是springMVC认为包下用了@controller注解的类是控制器-->
<context:component-scan base-package="com.it.controller" />
<!--注入SwaggerConfig配置-->
<bean class="com.it.config.SwaggerConfig" />
<!--所有Swagger目录的访问,直接访问localhost制定目录-->
<mvc:resources mapping="/swagger/**" location="/WEB-INF/swagger/" />
创建一个config包,在创建一个swagger的工具类(个人认为)
package com.it.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
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
@EnableWebMvc
@ComponentScan(basePackages = "com.it.controller")
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX项目接口文档")
.description("XXX项目接口测试")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("")
.build();
}
}
在contrller层使用
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/userController")
@Api(tags = "userController",description = "用户相关操作api")
public class UserController {
}
方法的实现(注意:要写方法的请求方式,如果不写请求方式,在测试接口的时候回自动生成各种形式请求的接口)
@ResponseBody
@RequestMapping( value = "/test", method = RequestMethod.POST)
@ApiOperation(value = "根据用户id主键查询用户信息", notes = "查询用户", httpMethod = "post", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiImplicitParams({
@ApiImplicitParam(name = "id", dataType = "int", required = true, value = "主键id", paramType = "query")
})
private List<User> test(Integer id, String name){
//方法引用
// Function<Integer, User> fun = userService::selectUserById;
//lamdba表达式
Function<Integer, User> fun = (x) -> {
return userService.selectUserById(x);
};
//删除
// userService.deleteUserById(id);
List<User> userList = userService.selectUser();
userList = userList.stream().skip(0 * 10).limit(10).collect(Collectors.toList());
return userList;
}
配置完重启项目,访问localhost:8080/swagger-ui.html即可测试接口