zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java读取文件并获得文件编码,转换为指定编码的工具类代码
    OpenStreetMap地图数据介绍(转)
    字符串匹配的KMP算法(转)
    Dijkstra算法求最短路径(java)(转)
    用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)
    用java解析在OpenStreetMap上下载的地图数据
    加载依赖的jar包在命令行编译和运行java文件
    CentOS 加载/挂载 U盘 (转)
    MongoDB:如何正常关闭服务(转)
    JTS(Geometry)(转)
  • 原文地址:https://www.cnblogs.com/zxf330301/p/9265474.html
Copyright © 2011-2022 走看看