Springboot+Swagger2

简介: springboot+swagger2http://start.spring.io/生成springboot工程引入maven依赖 io.

springboot+swagger2

http://start.spring.io/生成springboot工程

img_c0aa427a449f53ecc242d4d1a557c6f8.png

引入maven依赖

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

加载swagger2配置

@Configuration 
@EnableSwagger2 
public class SwaggerConfig {
  @Autowired
  Environment env;
  @Bean
  public Docket createRestApi(ApiInfo apiInfo) {
        return new Docket(DocumentationType.*SWAGGER_2*)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(env.getProperty("swagger.scan")))
                .paths(PathSelectors.any())
                .build();
  }

    @Bean
  public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(env.getProperty("swagger.title"))
                .description(env.getProperty("swagger.description"))
                .version(env.getProperty("swagger.version"))
                .build();
  }
}

application.yml

spring:
  application:
    name: swagger-test
server:
  port: 8080
  context-path: /api
swagger:
  scan: com.swagger.test
  title: 用户service API
  description: 用户service API
  version: 1.0.0

controller

@RestController
@RequestMapping("/user")
@Api(value="用户controller",description="用户操作",tags={"用户操作接口"})
public class UserController {
    @GetMapping("/get")
    @ApiOperation(value = "获取用户接口",notes = "获取用户接口notes",response = String.class)
    public String get(@ApiParam(name = "userName",value = "用户名",required = true) @RequestParam("userName") String userName){

        return "返回值:"+userName;
    }
    @PostMapping("/post")
    @ApiOperation(value = "获取用户接口Post",notes = "获取用户接口notes Post",response = UserForm.class)
    public UserForm post(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true)  UserForm userForm){
        return userForm;
    }
}

form

@ApiModel(description = "用户from")
public class UserForm {
    @ApiModelProperty(value = "用户名",name = "userName",required = true)
    private String userName;

    @Override
    public String toString() {
        return "UserForm{" +
                "userName='" + userName + '\'' +
                '}';
    }

    /**
     * Getter method for property <tt>userName</tt>
     *
     * @return property value of userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Setter method for property <tt>userName</tt>.
     *
     * @param userName value to be assigned to property userName
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

启动类:

@EnableSwagger2
@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

启动 访问:http://localhost:8080/api/swagger-ui.html

例子下载地址:http://download.csdn.net/download/zyj_2012/10191998

img_4ab634cec05a1a989452634548796fa5.png
目录
相关文章
|
1月前
|
Java API Spring
【一】springboot整合swagger
【一】springboot整合swagger
32 0
|
1月前
|
XML Java 测试技术
【二】springboot整合自定义swagger
【二】springboot整合自定义swagger
21 0
|
3月前
|
Java API Spring
Swagger使用-Spring Boot整合Swagger
Swagger使用-Spring Boot整合Swagger
45 0
|
1月前
|
Java Spring
Spring Boot3整合knife4j(swagger3)
Spring Boot3整合knife4j(swagger3)
143 1
|
2月前
|
Java API
SpringBoot 整合swagger3.X
SpringBoot 整合swagger3.X
|
4月前
|
Java API
SpringBoot中的Swagger2如何使用?
SpringBoot中的Swagger2如何使用?
22 1
|
2月前
|
Java 应用服务中间件 网络安全
Nginx配置静态页面+springboot应用+swagger+SSL的实现
Nginx配置静态页面+springboot应用+swagger+SSL的实现
|
2月前
|
Java Maven
【SpringBoot专题_02】springboot集成Swagger详细教程
【SpringBoot专题_02】springboot集成Swagger详细教程
|
4月前
|
Java API
SpringBoot集成Swagger2组件
SpringBoot集成Swagger2组件
28 1

热门文章

最新文章