第一次是用Swagger,公司一直在用这个api,这两天自己按着百度上搭建了下。
官网长这个亚子:https://swagger.io/
我也是按着百度上很多博客学习的,第一次使用,出现了很多小问题。
第一步maven导包 ,用的是2.2.2,我也不知道有什么版本的,额。。管他呢,出来效果就行
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.2.2</version>
</dependency>
写一个Swagger的配置类。唯一注意的就是这个扫描的路径了。别的没什么
@Configuration
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//扫描的controller ,改成本地路径
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 使用 Swagger2 构建RESTful API")
//创建人
.contact("Zhang")
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
在Application启动类里加注解 @EnableSwagger2
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
正常的写实体啊,service,controller,我用的mybatis的逆向生成,大概贴一下代码。差不多都是生成的代码
@Mapper
public interface SysUserDao {
List<SysUser> selectAll();
int deletes(List list);
int deleteByPrimaryKey(Integer id);
int insert(SysUser record);
int insertSelective(SysUser record);
SysUser selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(SysUser record);
int updateByPrimaryKey(SysUser record);
Page<SysUser> findByPaging();
}
@Service
public class SysUserService {
@Autowired
private SysUserDao sysUserDao;
public List selectAll(){
return sysUserDao.selectAll();
}
public int insertSelective(SysUser record){
return sysUserDao.insertSelective(record);
}
public int deleteByPrimaryKey(Integer id){
return sysUserDao.deleteByPrimaryKey(id);
}
public Page<SysUser> findByPaging(Integer pageNum,Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
Page list=sysUserDao.findByPaging();
return list;
}
}
这里有个分页查询,是用的pagehelper分页插件,也是百度百度就出来了,很简单。
@Controller
@Api(value = "用户接口")
@RequestMapping("/User")
public class SysUserController {
@Autowired
private SysUserService sysUserService;
@ResponseBody
@RequestMapping("/listAll")
@ApiOperation(value="获取用户列表", notes="获取用户列表")
public List selectAll(){
return sysUserService.selectAll();
}
@ResponseBody
@RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
@ApiOperation(value="获取分页用户列表", notes="获取分页用户列表")
//@ApiImplicitParam(name = "pageNum",value = "起始页",required = true,dataType = "Int",paramType = "path")
public List findByPaging(@ApiParam(name = "pageNum",value = "起始页",required = true)@RequestParam(name = "pageNum", required = true)Integer pageNum,
@ApiParam(name = "pageSize",value = "当前页数",required = true)@RequestParam(name = "pageSize", required = true) Integer pageSize){
return sysUserService.findByPaging(pageNum,pageSize);
}
@RequestMapping("/insert")
@ResponseBody
@ApiOperation(value="添加用户", notes="添加用户")
public int insertSelective(@RequestBody SysUser record){
return sysUserService.insertSelective(record);
}
@RequestMapping(value = "/delete",method = {RequestMethod.DELETE ,RequestMethod.GET})
@ResponseBody
@ApiOperation(value="删除用户", notes="删除用户")
//@ApiImplicitParam(name = "id",value = "用户",required = true,dataType = "Int",paramType = "path")
public int deleteByPrimaryKey(@ApiParam(name = "id",value = "id",required = true)@RequestParam(name = "id", required = true)Integer id){
System.out.println("id"+id);
return sysUserService.deleteByPrimaryKey(id);
}
}
里面注解就那几个,我也没搞懂
运行下就长这个样子
出问题的地方有,请求类型,地址栏上可以正常请求查询数据接口,但是有条件,需要传参数的就会报错,调试发现传值到后台参数都为null,最骚的是我新建页面ajax的请求是没问题的,地址栏请求也是没问题的,就是经过Swagger的请求传过去为null.我在需要传参数的加上@RequestParam来接受传参的类型数值。就接收到swagger的参数了。
在添加接口的时候,参数是个对象,用@RequestBody注解,页面这个接口也出现了右边的黄框框,点下就不用自己来写参数。在mapper.xml文件做了判断,传一个也是可以加进去的。
第一次写博客,记录下自己出现的点问题,这个技术是刚接触的,希望自己能以后更加坚持,有问题肯定是可以解决的。