码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+swagger2案例

时间:2018-07-04 22:55:15      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:ati   builder   程序猿   ons   tle   cto   ret   plugins   frame   

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

springboot+swagger2案例

标签:ati   builder   程序猿   ons   tle   cto   ret   plugins   frame   

原文地址:https://www.cnblogs.com/zxf330301/p/9265474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!