前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用dropwizard(5)--加入swagger

使用dropwizard(5)--加入swagger

作者头像
Ryan-Miao
发布2018-03-14 10:37:20
1.1K0
发布2018-03-14 10:37:20
举报
文章被收录于专栏:Ryan MiaoRyan Miao

前言

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

Demo source

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

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

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

添加swagger依赖

代码语言:javascript
复制
<dependency>
    <groupId>com.smoketurner</groupId>
    <artifactId>dropwizard-swagger</artifactId>
    <version>1.1.2-1</version>
</dependency>

在configuration中新增swagger的基础配置

代码语言:javascript
复制
@JsonProperty("swagger")
private SwaggerBundleConfiguration swaggerBundleConfiguration;

在配置文件中,新增

代码语言:javascript
复制
swagger:
  resourcePackage: com.test.domain.resource
  schemes:
    - http

新增SwaggerBundle

创建com.test.bundles.SwitchableSwaggerBundle

代码语言:javascript
复制
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新增

代码语言:javascript
复制
bootstrap.addBundle(new SwitchableSwaggerBundle());

修改Resource类

代码语言:javascript
复制
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, 结果如下:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Demo source
  • 添加swagger依赖
  • 在configuration中新增swagger的基础配置
  • 新增SwaggerBundle
    • 引入SwaggerBundle
    • 修改Resource类
    • install&Run
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档