前言

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,

结果如下:

最新文章

  1. 基于spring和Quartz定时器
  2. 无法启动:此实现不是Windows平台FIPS验证的加密算法的一部分
  3. Beaglebone Back学习四(GPIO实验)
  4. xcopy 复制了0个文件
  5. [NOIP2011]瑞士轮
  6. 在线支付接口之PHP支付宝接口开发简单介绍
  7. SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
  8. APP反编译第一课《如何找到核心代码》
  9. 【续】抓个Firefox的小辫子,jQuery表示不背这黑锅,Chrome,Edge,IE8-11继续围观中
  10. UNIX环境高级编程——线程同步之互斥量
  11. python书籍推荐:Python Cookbook第三版中文
  12. 如何将WORD表格转换成EXCEL表格
  13. linux 安装node.js 和npm
  14. Spring Security(六):2.3 Release Numbering
  15. Linux 网络侦错:无法联机原因分析
  16. Mysql Window 解压版 忘记密码
  17. 20145232 韩文浩 《Java程序设计》第4周学习总结
  18. vue单页面应用项目优化总结(转载)
  19. ARKit从入门到精通(6)-ARSession介绍
  20. 【Linux笔记】在后台执行scp,实现服务器间无密码文件拷贝。

热门文章

  1. 关于WPF的验证
  2. windows mysql 操作实践
  3. TableView的性能优化
  4. 阻塞IO
  5. Awesome Projects (汇聚全球所有&#128046;项目,你值得拥有)
  6. java中集合类HashSet、ArrayList、LinkedList总结
  7. python第二课
  8. 在WebBrowser控件使用js调用C#方法
  9. java实现在线文档浏览
  10. Linux下Git安装、配置