zoukankan      html  css  js  c++  java
  • spring boot2集成api文档工具swagger-ui(上)

    说明

    第一步:创建项目

    第二步:导入开发工具

    • 打开下载目录,解压项目文件

    • 启动idea,引入项目文件

    第三步:引入swagger-ui包

        <dependency>
    			<groupId>io.springfox</groupId>
    			<artifactId>springfox-swagger2</artifactId>
    			<version>2.9.2</version>
    	</dependency>
    	<dependency>
    	    <groupId>io.springfox</groupId>
    		<artifactId>springfox-swagger-ui</artifactId>
    		<version>2.9.2</version>
    	</dependency>
    

    第四步:创建一个swagger 配置

    @EnableSwagger2
    @Configuration
    public class SwaggerConfig {
    
    
        @Bean
        public Docket api(){
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.shelton.swaggerui"))
                    .build();
        }
    
    
        public ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("api接口说明")
                    .version("1.0.0")
                    .build();
        }
    
    }
    

    第五步:创建一个POST api接口

    
    @RestController
    @Api(value = "用户接口", tags={"用户操作接口"})
    public class IndexController {
    
        @ApiOperation(value = "用户登录",notes = "登录",httpMethod = "POST")
        @PostMapping("/login")
        public Object get(@RequestParam @ApiParam(name="username",value="登录账号",required=true) String username, @RequestParam @ApiParam(name="password",value="密码",required=true) String password){
    
            Map map = new HashMap();
            map.put("msg","success");
            map.put("code","1");
            return map;
        }
    }
    
    • 页面效果图

    • 测试api接口请求

    • 这里返回的结果,可以看到,跟我们预期返回的是一样。

    总结

  • 相关阅读:
    spring-mvc dispatcherServlet
    常用注解
    spring基础
    消息转换
    高级装配
    Leetcode第242题:有效的字母异位词
    Leetcode第76题:最小覆盖子串
    Leetcode633题平方数之和
    Leetcode454题四数之和II
    java从虚拟机执行角度解析案例(转)
  • 原文地址:https://www.cnblogs.com/qinshengfei/p/12182288.html
Copyright © 2011-2022 走看看