使用dropwizard(5)--加入swagger

简介: 前言Swagger已经成API service的规范了,本处在dropwizard中简单集成Swagger.Demo sourcehttps://github.com/Ryan-Miao/l4dropwizard本文是基于dropwizard入门之上的演进。

img_17e764d4d3011ebd0a2a577f51ad1293.png
img_30f8eaa6b41e2e0af4c9b5ee117b234d.png

前言

Swagger已经成API service的规范了,本处在dropwizard中简单集成Swagger.

Demo source

https://github.com/Ryan-Miao/l4dropwizard

本文是基于dropwizard入门之上的演进。

确保依赖都是最新的,或者自行解决版本冲突,比如jackson不同版本之间的类有所不同。

添加swagger依赖

<dependency>
    <groupId>com.smoketurner</groupId>
    <artifactId>dropwizard-swagger</artifactId>
    <version>1.1.2-1</version>
</dependency>

在configuration中新增swagger的基础配置

@JsonProperty("swagger")
private SwaggerBundleConfiguration swaggerBundleConfiguration;

在配置文件中,新增

swagger:
  resourcePackage: com.test.domain.resource
  schemes:
    - http

新增SwaggerBundle

创建com.test.bundles.SwitchableSwaggerBundle

package com.test.bundles;

import com.test.configuration.HelloWorldConfiguration;
import io.dropwizard.setup.Environment;
import io.federecio.dropwizard.swagger.SwaggerBundle;
import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration;

public class SwitchableSwaggerBundle extends SwaggerBundle<HelloWorldConfiguration> {

    @Override
    protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(HelloWorldConfiguration configuration) {
        return configuration.getSwaggerBundleConfiguration();
    }

    @Override
    public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception {
        super.run(configuration, environment);
    }
}

引入SwaggerBundle

com.test.HelloWorldApplication#initialize新增

bootstrap.addBundle(new SwitchableSwaggerBundle());

修改Resource类

package com.test.domain.resource;

import com.codahale.metrics.annotation.Timed;
import com.test.domain.entiry.GithubUser;
import com.test.domain.service.IGithubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Created by Ryan Miao on 9/14/17.
 */
@Api("/github")
@Path("/github")
@Produces(MediaType.APPLICATION_JSON)
public class GithubResource {

    private IGithubService service;

    @Inject
    public GithubResource(IGithubService service) {
        this.service = service;
    }

    @GET
    @Timed
    @Path("/users/{username}")
    @ApiOperation(value = "Get github user profile.", notes = "There should be the note.")
    @ApiResponses({
            @ApiResponse(code = 401, message = "Valid credentials are required to access this resource."),
            @ApiResponse(code = 400, message = "Params not valid."),
            @ApiResponse(code = 500, message = "Something wrong from the server."),
            @ApiResponse(code = 200, message = "Success.", response = GithubUser.class)
    })
    public GithubUser getUserProfile(@PathParam("username") final String username) {
        return service.getUserProfile(username);
    }

}

install&Run

浏览器访问http://localhost:8080/swagger,
结果如下:

img_b431f75f3a42a8162fad10d276af86f6.png





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
1月前
|
Java API Spring
springboot集成swagger
这篇文章介绍了如何在Spring Boot项目中集成Swagger 2.10.0来生成API文档,包括添加依赖、编写配置类、创建接口文档,并使用Knife4j美化Swagger界面。
|
4月前
|
JSON 前端开发 Java
Swagger介绍及使用
Swagger介绍及使用
100 2
|
4月前
|
存储 API Go
Hertz 整合swagger
Hertz 整合swagger
77 0
|
前端开发 Java API
springboot 集成swagger
springboot 集成swagger
114 0
|
前端开发 Java API
Swagger详解
Swagger详解
138 0
|
前端开发 Java API
|
数据可视化 前端开发 Java
swagger的使用
(1)@Api:用在类上,例如Controller,表示对类的说明 (2)@ApiModel:用在类上,通常是实体类,表示一个返回响应数据的信息 (3)@ApiModelProperty:用在属性上,描述响应类的属性 (4)@ApiOperation:用在请求方法上,说明方法的用途、作用 (5)@ApiImplicitParams:用在请求的方法上,表示一组参数的说明 (6)@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
124 0
swagger的使用
|
数据可视化
Swagger快速使用
Swagger快速使用
|
前端开发 Java fastjson
SpringBoot中集成Swagger
最近在做一个demo,出一些测试接口,就想到了集成swagger。文章对swagger2和swagger3的集成记录
799 0
SpringBoot中集成Swagger
|
SpringCloudAlibaba 微服务
SpringCloudAlibaba篇(八)SpringCloudGateWay聚合swagger3、SpringBoot2.6.X整合swagger3+knife4j
SpringCloudAlibaba篇(八)SpringCloudGateWay聚合swagger3、SpringBoot2.6.X整合swagger3+knife4j
2043 0
SpringCloudAlibaba篇(八)SpringCloudGateWay聚合swagger3、SpringBoot2.6.X整合swagger3+knife4j