博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用dropwizard(5)--加入swagger
阅读量:7049 次
发布时间:2019-06-28

本文共 2886 字,大约阅读时间需要 9 分钟。

前言

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

Demo source

本文是基于之上的演进。

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

添加swagger依赖

com.smoketurner
dropwizard-swagger
1.1.2-1

在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
{ @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,

结果如下:

swagger.png

本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/dropwizard-with-swagger-example.html,如需转载请自行联系原作者
你可能感兴趣的文章
setContentType 的顺序 导致的乱码问题
查看>>
xcode不能智能提示问题
查看>>
Maven Dependencies - miss
查看>>
Mongo Collections
查看>>
Android MVVM开发——DataBinding基础
查看>>
php中file_get_content 和curl以及fopen
查看>>
基于 Pusher 驱动的 Laravel 事件广播(上)
查看>>
fuel部署openstack 打开fuel的UI界面出现白屏的情况
查看>>
PhpStrom安装Xdebug调试工具
查看>>
Spark Streaming源码解读之数据清理 内幕
查看>>
项目打包流程
查看>>
vue-cli项目动态引用图片链接问题
查看>>
合格程序员每天每周每月每年应该做的事
查看>>
macbook pro(2012款)更换ssd硬盘(光驱拆除换成ssd,原有硬盘仍然使用)
查看>>
Stream API(三)--原始类型流特化
查看>>
使用webiopi控制树莓派的GPIO引脚电平(WEB在线管理)
查看>>
js中call与apply
查看>>
隐式转换
查看>>
(转)直接拿来用!最火的iOS开源项目(二)
查看>>
mybatis 实体嵌套查询
查看>>