1、POM.xml 加入依赖包:(采用 swagger2,)
<!--swagger2相关-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--json相关-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
2、编写 swagger2 的配置类
@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "controller")
public class SwaggerConfig {
@Bean
pu blic Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("项目接口文档").description("bingo").version("1.0.0").termsOfServiceUrl("")
.license("").licenseUrl("").build();
}
}
3、配置 springMvc 相关文件, 在原有基础上加上如下代码
<bean class="config.SwaggerConfig"/><!--刚才写的swagger配置类-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/><!--必须-->
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/><!--必须-->
4、编写 controller 测试:
@RestController
@RequestMapping(value = "/userInfo")
@Api(value = "用户信息")
public class UserInfoController {
private Logger logger = LoggerFactory.getLogger(getClass());
@ResponseBody
@RequestMapping(value = "/selectAllUsers", method = RequestMethod.GET)
@ApiOperation(value = "查询所有的人员信息并分页展示", notes = "查询所有的人员信息并分页展示")
@ApiImplicitParams({
@ApiImplicitParam(name = "page",value = "跳转到的页数", required = true, paramType = "query"),
@ApiImplicitParam(name = "size",value = "每页展示的记录数", required = true, paramType = "query")
})
public ServerResponse selectAllUsers(Integer page, Integer size) {
return ServerResponse.createServerResponseBySuccess("sueess");
}
@ResponseBody
@RequestMapping(value = "/selectContacts", method = RequestMethod.GET)
@ApiOperation(value = "查询通讯录人员信息", notes = "查询通讯录人员信息")
public ServerResponse selectContacts() {
return ServerResponse.createServerResponseBySuccess("sueess");
}
}
5、启功项目输入访问路径:http://localhost:8080/test/swagger-ui.html (test 为项目名)
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于