• springboot+swagger2案例


    1.pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>swagger</groupId>
      <artifactId>swagger</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      
        <parent>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-parent</artifactId>  
            <version>1.4.3.RELEASE</version>  
            <relativePath/> <!-- lookup parent from repository -->  
        </parent>  
        
        
        <properties>  
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
            <java.version>1.8</java.version>  
        </properties>  
        
        
        <dependencies>  
        		<dependency>
      		 <groupId>org.springframework.boot</groupId>
      		 <artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		
    		<dependency>
          		 <groupId>io.springfox</groupId>
        				<artifactId>springfox-swagger2</artifactId>
       			 <version>2.6.0</version>
    		</dependency>
    		<dependency>
       			 <groupId>io.springfox</groupId>
        			<artifactId>springfox-swagger-ui</artifactId>
        			<version>2.6.0</version>
    		</dependency>
    
    		
    	</dependencies>
    </project>
    

      swagger2配置文件

    package com.newtouch.swagger.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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
    
    	
    	@Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.newtouch.swagger.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("SpringBoot中使用Swagger2构建RESTfulAPIs")
                    .description("SpringBoot中使用Swagger2构建RESTfulAPIs")
                    .termsOfServiceUrl("http://blog.didispace.com/")
                    .contact("程序猿DD")
                    .version("1.0")
                    .build();
        }
    
    
    }
    

      请求bean

    package com.newtouch.swagger.controller.bean;
    
    
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    @ApiModel("用户")
    public class UserRequest {
    
    	@ApiModelProperty(value="用户id",name="id",required=false)
    	private int id;
    	
    	@ApiModelProperty(value="用户name",name="name",required=false)
    	private String name;
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	
    
    }
    

      controller

    package com.newtouch.swagger.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.newtouch.swagger.controller.bean.UserRequest;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    
    @Api("用户操作类")
    @RestController
    public class UserController {
    
    	
    	
    	
    	@RequestMapping(value="/getName")
    	@ApiOperation("用户对象")
    	public Map getName(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) UserRequest u) {
    		Map<String, Object> resultMap = new HashMap<>();
    		resultMap.put("id", u.getId());
    		resultMap.put("name", u.getName());
    		
    		return resultMap;
    	}
    	
    }
    

      效果展示

    swagger注解详解:https://blog.csdn.net/u014231523/article/details/76522486

  • 相关阅读:
    PTA甲级—树
    PTA甲级—链表
    PTA刷题记录
    PTA甲级—数学
    PTA甲级—常用技巧与算法
    PAT甲级—暴力搜索
    Qt连连看(三)非核心功能实现
    常见数据类型取值范围与10的数量级对照
    PTA甲级—STL使用
    Qt连连看(二)界面制作
  • 原文地址:https://www.cnblogs.com/zxf330301/p/9265474.html
Copyright © 2020-2023  润新知