分三步
第一步:
创建一个公共的子模块,在要用到swegger的地方,在子模块的pom文件中引入相关的依赖
<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>
第二步:
创建一个swegger的配置类
package com.example.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author zwj
* @date 2021/4/19 16:00
* @description
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
// 指定构建api文档的详细信息的方法:apiInfo()
.apiInfo(apiInfo())
.select()
// 指定要生成api接口的包路径
.apis(RequestHandlerSelectors.basePackage("com.example.control"))
//使用了 @ApiOperation 注解的方法生成api接口文档
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
//可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
/**
* 设置api文档的详细信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多请关注http://www.baidu.com")
.termsOfServiceUrl("http://www.baidu.com")
.contact("sunf")
.version("1.0")
.build();
}
}
第三步,在要用的swegger的控制层地方引入依赖
package com.example.control;
import com.example.bean.Userbean;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @author zwj
*/
@RequestMapping("/test")
@RestController
@Api(value = "用户信息接口")
public class TestController {
/**
* 将Service注入Web层
*/
@Resource
UserService userService;
@GetMapping("/list")
@ApiOperation("/获取用户列表")
@ApiImplicitParam(name = "id", value = "id", required = false, paramType = "query", dataType = "String")
public List<Userbean> get(Integer id, String name, String password) {
List<Userbean> userbeanList = userService.loginIn(id, name, password);
return userbeanList;
}
service层
package com.example.service;
import com.example.bean.Userbean;
import java.util.List;
import java.util.Map;
/**
* @author zwj
*/
public interface UserService {
List<Userbean> loginIn(Integer id, String name, String password);
}
impl
package com.example.impl;
import com.example.bean.Userbean;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zwj
*/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
/**
* 将DAO注入Service层
*/
@Resource
private UserMapper userMapper;
@Override
public List<Userbean> loginIn(Integer id, String name, String password) {
List<Userbean> info = userMapper.getInfo(id, name, password);
return info;
}
mapper
package com.example.mapper;
import com.example.bean.Userbean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
List<Userbean> getInfo(Integer id, String name, String password);
}
xml
<select id="getInfo" parameterType="list" resultType="com.example.bean.Userbean">
SELECT *
FROM user
</select>
swagger:的默认访问地址
http://localhost:8080/swagger-ui.html
效果就是这样的