热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Springboot中使用Swagger2构建RESTfulAPIs

变态的问题:在一台电脑上context-path不能是根这里设置contextapplication.propertiesserver.servlet.cont

变态的问题:在一台电脑上

context-path 不能是根

这里设置context

application.properties

server.servlet.context-path=/jk

1、pom.xml

增加

io.springfoxspringfox-swagger22.9.2

io.springfoxspringfox-swagger-ui2.9.2

2、配置

SwaggerConfig.java

package com.example.demo.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** @author yujikuan*/
@EnableSwagger2
@Configuration
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Springboot 中使用Swagger2构建RESTful APIs").termsOfServiceUrl("https://blog.csdn.net/qq_26229005/article/details/103843497").description("springmvc swagger2").contact(new Contact("yujikuan", "https://blog.csdn.net/qq_26229005/article/details/103843497", "329656576@qq.com")).version("1.1").build();}
}

 

Controller

package com.example.demo.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api")
@Api(value = "api接口", description="用户相关操作")
@Slf4j
public class ApiController {@RequestMapping(value = "/user")@ApiOperation(value = "用户查询服务", notes = "根据传过来的user_id来查询用户")@ResponseBody
//注释声明 RequestParampublic User getUserById(@ApiParam(value = "用户id") @RequestParam String user_id){/* User user = userService.getUserById(user_id);map.put("user", user);*/log.info("userid="+user_id);User user=new User();user.setUser_id(user_id);user.setUser_name("于吉宽");user.setPassword("dsfsfs@ddd");return user;}}

 

实体类型

package com.example.demo.controller;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;@ApiModel(value = "用户")public @Data class User {private String user_id;@ApiModelProperty(value = "用户名")private String user_name;@ApiModelProperty(value = "密码")private String password;
}

最后启动项目

访问http://localhost:8080/jk/swagger-ui.html#/

如果报curl错误,请https://curl.haxx.se/download.html#Win64 下载,配置相关环境变量既可。

参考

http://springfox.github.io/springfox/docs/current/#introduction


推荐阅读
author-avatar
zevbin_187
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有