前言

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,

结果如下:

使用dropwizard(5)--加入swagger的更多相关文章

  1. 使用swagger作为restful api的doc文档生成

    初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...

  2. 使用dropwizard(6)-国际化-easy-i18n

    前言 Dropwizard官方文档并没有提供国际化的模块,所以只能自己加.Spring的MessageResource用的很顺手,所以copy过来. Easy i18n 在整合Dropwizard的时 ...

  3. Dropwizard入门及开发步骤

    Dropwizard介绍 Dropwizard结构的服务组成 开发步骤 Dropwizard介绍 Dropwizard是一个微服务框架, 是各项技术的一个集成封装.它包含了以下组件: 嵌入式Jetty ...

  4. ABP框架 - Swagger UI 集成

    文档目录 本节内容: 简介 Asp.net Core 安装 安装Nuget包 配置 测试 Asp.net 5.x 安装 安装Nuget包 配置 测试 简介 来自它的网页:“...使用一个Swagger ...

  5. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  6. 用Swagger生成接口文档

    Swagger简介 在系统设计的时候,各个应用之间往往是通过接口进行交互的.因此接口的定义在整个团队中就变得尤为重要.我们可以把接口的规范用接口描述语言进行描述,然后Swagger可以根据我们定义的接 ...

  7. 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)

    对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...

  8. ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档

    原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...

  9. 在ASP.NET Core Web API上使用Swagger提供API文档

    我在开发自己的博客系统(http://daxnet.me)时,给自己的RESTful服务增加了基于Swagger的API文档功能.当设置IISExpress的默认启动路由到Swagger的API文档页 ...

随机推荐

  1. iOS如何提高页面流畅度

    A.提高CPU性能 对象创建1.尽量用轻量的对象代替重量的对象,比如CALayer 比 UIView 要轻量许多,如果不考虑交互事件的话,可以选择CALayer.2.Storyboard和xib加载对 ...

  2. [解读REST] 1.REST的起源

    0. 世界上第一个网站 1990年12月20日,这一天对于现在的互联网来说意义非凡.欧洲核子研究组织(CREN)的科学家Tim Berners-Lee在一台NeXT电脑上启动了世界上的第一个网站(当然 ...

  3. 从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD

    第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分 ...

  4. hdu5696 区间的价值

    区间的价值 我们定义"区间的价值"为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R-L+1). 现在聪明的杰西想要知道,对于长度为k的区间,最大 ...

  5. MYSQL数据库引擎区别详解

    数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...

  6. d3力导向图聚焦

    效果描述 双击节点,节点以及节点一度关联的节点保持高亮状态,其余节点变灰,半径变小,文字消失,并且向内收缩. 效果展示 正常状态 聚焦效果 关键代码 节点变化 激活节点保持高亮的样式,其余节点应用no ...

  7. leetcode 697. Degree of an Array

    题目: Given a non-empty array of non-negative integers nums, the degree of this array is defined as th ...

  8. 调试 ms 源代码

    如果需要调试 WPF 源代码或框架源代码,那么需要使用 DotPeek. 首先需要下载 dotPeek ,可以到官网下载 dotPeek: Free .NET Decompiler & Ass ...

  9. jemalloc 快速上手攻略

    引言 - 赠送个 Cygwin (加精) Cygwin 有它存在的合理性. 至少比 wine 好太多了. 它主要功能是在winds上面简易的模拟出linux环境, 比虚拟机 轻量一点点. 坑也不少, ...

  10. MTV模型

    django的MTV分别代表: model(模型):负责业务对象与数据库的对象(orm) template(模板):负责把页面展示给用户 view(视图):负责业务逻辑,并在适当的时候调用model和 ...