Kotlin+SpringBoot服务端restful框架搭建(3)简单集成swagger2

添加依赖

在之前的的项目添加这些swagger2依赖的类
 
   <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>
注意:本文只是简单的做集成,目前尝试可以正常启动显示接口信息,但是对于部分嵌套的注解添加会报错,目前还没找到解决方案。 @APiresponses()和@ApiImplicitParams()使用时回报错。
 
添加swagger2配置类

这个配置springboot中使用swagger2类似
package com.kotliner.demo
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

@Configuration
@EnableSwagger2
class Swagger{


@Bean
fun createRestApi():Docket{
var docket = Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("业务系统API接口文档")
.select()
.apis(RequestHandlerSelectors.basePackage("com.kotliner.demo"))
.paths(PathSelectors.any())
.build();
return docket;
}

fun apiInfo():ApiInfo{

var api = ApiInfoBuilder()
.title("业务系统名称")
.description("swagger2构建框架restfull API文档")
.contact("猫")
.version("1.0")
.termsOfServiceUrl("")
.build();
return api;

}


}

conroller添加swagger2注解
 
目前只使用了@Api,@ApiOperation,@ApiPara,添加完成后我们就可以启动了。
 
package com.kotliner.demo.controller

import com.kotliner.demo.entity.CaOrder
import com.kotliner.demo.mapper.OrderMapper
import com.kotliner.demo.service.ICaOrderService
import io.swagger.annotations.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@Api(value = "order",description = "订单业务")
@RestController
class OrderController{

@Autowired
lateinit var service: ICaOrderService;
@Autowired
lateinit var mapper: OrderMapper


@ApiOperation(value = "订单查询",notes = "订单列表查询")
@RequestMapping(value = "/findAllOrder",method = arrayOf(RequestMethod.POST))
fun findAllOrder(@ApiParam(name="order",value = "订单",required = true) @RequestBody order: CaOrder):List<CaOrder>{
return service.findAllOrder(order);
}

@ApiOperation(value = "订单查询",notes = "订单列表查询")
@RequestMapping(value = "/find",method = arrayOf(RequestMethod.POST))
fun find(@ApiParam(name="order",value = "订单",required = true) @RequestBody order: CaOrder):String{
var orders: List<CaOrder> = mapper.findOrderList(order);


return "ssssssssssss"+orders.toString();
}

@ApiOperation(value = "新增订单",notes = "新增订单")
@RequestMapping(value = "/save",method = arrayOf(RequestMethod.POST))
fun save(@ApiParam(name="order",value = "订单",required = true) @RequestBody order: CaOrder):Int{
return service.saveOrder(order);
}

@ApiOperation(value = "修改订单",notes = "修改订单")
@RequestMapping(value = "/update",method = arrayOf(RequestMethod.POST))
fun update(@ApiParam(name="order",value = "订单",required = true) @RequestBody order: CaOrder):Int{
return service.updateOrder(order);
}


@ApiOperation(value = "根据id删除订单",notes = "根据id删除订单")
@RequestMapping(value = "/delete",method = arrayOf(RequestMethod.GET))
fun delete(@ApiParam(name="id",value = "订单id",required = true) @RequestParam(value = "id") id:Int){
service.deleteOrder(id);
}
}
访问

输入http://localhost:8090/demo/swagger-ui.html地址就可以看到了,新版本的swagger界面做了改版。
1.png 2.png

3 个评论

项目地址:https://gitee.com/mdtx/kotlinDemo
Vision

Vision 回复 Cat

提示访问权限受限
不错不错,收藏了

要回复文章请先登录注册