当前位置:   article > 正文

Swagger2 的配置与简单运用_swagger2配置

swagger2配置

一. Swagger2 配置

springboot 目录下新建 config 文件夹

  1. @Bean
  2. public Docket createRestApi() {
  3. return new Docket(DocumentationType.SWAGGER_2)
  4. .apiInfo(apiInfo())
  5. //是否开启 (true 开启 false隐藏。生产环境建议隐藏)
  6. //.enable(false)
  7. .select()
  8. //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
  9. .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
  10. //指定路径处理PathSelectors.any()代表所有的路径
  11. .paths(PathSelectors.any())
  12. .build();
  13. }
  14. private ApiInfo apiInfo() {
  15. return new ApiInfoBuilder()
  16. //设置文档标题(API名称)
  17. .title("SpringBoot中使用Swagger2接口规范")
  18. //文档描述
  19. .description("接口说明")
  20. //服务条款URL
  21. .termsOfServiceUrl("http://localhost:8080/")
  22. //版本号
  23. .version("1.0.0")
  24. .build();

二.Swagger2进入方法

http://localhost:8080/swagger-ui.html(8080为自己端口号)

三.简单运用

在springboot项目的xml文件中插入post配置

示例如下

  1. <insert id="add" parameterType="com.example.demo.test.intellect" >
  2. insert into intellectualproperty(companyid, `time`, kind, `name`, material, updatetime) value(#{companyid},#{time},#{kind},#{name},#{material},#{updatetime})
  3. </insert>

其中 intellectualproperty 为数据库表名()中为对象浏览器中各参数名

在mapper中添加

boolean add(intellect intellect);

service中对应添加

boolean add(intellect intellect);

最后在controller中添加

  1. @PostMapping("/add")
  2. public boolean add(@RequestBody intellect intellect){
  3. return intellservice.add(intellect);

然后运行成功后进入

http://localhost:8080/swagger-ui.html(8080为端口号)

点击intell(对应项目)

点击add Post

点击Try it out

即可实现增删改查中的增(POST)

ps:应删除“id”:0,因为功能实现后会自动生成id

运行成功示例

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号