jetty服务器jersey框架使用swagger生成API文档

1.下载swaggerui,放入工程resource下

GitHub-swagger-ui

注意编辑index.html

      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = window.location.toString();
        url = url.substring(0, url.lastIndexOf("/docs")) + "/test/swagger.json";
      }

2.编辑POM

    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.swagger/swagger-jersey2-jaxrs -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.6.2</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
<!-- 不一起打包,手动复制到jar同级目录
            <resource>
                <directory>src/main/resources/swaggerui</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
                <targetPath>swaggerui/</targetPath>
            </resource>
-->
        </resources>
    </build>

3.编辑Main.java,添加两个函数。

    import io.swagger.jaxrs.config.BeanConfig;

    private static ContextHandlerCollection createContextHandlerCollection() throws Exception{}
    改为
    /**
     * 创建handler
     *
     * @param properties
     * @return
     * @throws Exception
     */
    private static ContextHandlerCollection createContextHandlerCollection() throws Exception {
        String path = "/hi";
        boolean isSwaggerEnabled = true;
        ContextHandler apiContextHandler = buildContextHandler(path);
        
        if (isSwaggerEnabled) {
            ContextHandler swaggerContextHandler = buildSwaggerUI(path, "com.test.rest");
            return new ContextHandlerCollection(
                    apiContextHandler, swaggerContextHandler
            );
        }
        return new ContextHandlerCollection(apiContextHandler);
    }


    /**
     * resource handler : swagger文档
     *
     * @param base_path
     * @param resourcePackage
     * @return
     * @throws Exception
     */
    private static ContextHandler buildSwaggerUI(String base_path, String resourcePackage) throws Exception {
        // This configures Swagger  
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setResourcePackage(resourcePackage);
        beanConfig.setScan(true);
        beanConfig.setBasePath(base_path);
        beanConfig.setDescription("Entity Browser API to demonstrate Swagger with Jersey2 in an "
                + "embedded Jetty instance, with no web.xml or Spring MVC.");
        beanConfig.setTitle("Entity Browser");
        beanConfig.setFilterClass(null);

        final ResourceHandler swaggerUIResourceHandler = new ResourceHandler();
        swaggerUIResourceHandler.setResourceBase("swaggerui");
        final ContextHandler swaggerUIContext = new ContextHandler();
        swaggerUIContext.setContextPath("/docs");
        swaggerUIContext.setHandler(swaggerUIResourceHandler);
        return swaggerUIContext;
    }

4.编辑MyJerseyConfig.java

import io.swagger.jaxrs.listing.ApiListingResource;

//packages("com.test.mavenproject.rest");

改为

packages("com.test.rest", ApiListingResource.class.getPackage().getName());

5.编辑服务类

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Path("gett")
@Api("GET测试")
public class GetTestResource {

    @Inject
    public GetTestResource() {

    }

    @GET
    @Path("b")
    @ApiOperation(value = "返回json",
            notes = "错误信息:<br/>"
            + "error:{code = \"serverError\", message = \"not support\"}<br/>",
            response = GetTestResponse.class)
    @Produces(MediaType.APPLICATION_JSON)
    public GetTestResponse b() {
        GetTestResponse response = new GetTestResponse();
        response.setVal(123);
        return response;
    }
}



package com.test.rest.gettest;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 *
 * @author Admin
 */
@ApiModel(value = "GetTestResponse")
public class GetTestResponse {

    @ApiModelProperty(value = "数值")
    @JsonProperty("val")
    private int val;

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
}

更详细的Api介绍All Classes(swagger-annotation)

完成后访问 http://localhost:8080/docs/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Jetty 欢迎访问Jetty文档 Wiki. Jetty是一个开源项目,提供了http服务器、http客户端和java servlet容器。 这个wiki提供jetty的入门教程、基础配置、功能特性、优化、安全、JavaEE、监控、常见问题、故障排除帮助等等。它包含教程、使用手册、视频、特征描述、参考资料以及常见问题。 Jetty文档 ---------------- 入门: 下载Download, 安装, 配置, 运行 Jetty入门(视频) 下载和安装Jetty 如何安装一个Jetty包 如何配置Jetty – 主要文档 如何运行Jetty 用JConsole监控Jetty 如何使用Jetty开发 Jetty HelloWorld教程 Jetty和Maven HelloWorld教程 Jetty(6)入门 (www.itjungle.com) Jetty Start.jar 配置Jetty 如何设置上下文(Context Path) 如何知道使用了那些jar包 如何配置SSL 如何使用非root用户监听80端口 如何配置连接器(Connectors) 如何配置虚拟主机(Virtual Hosts) 如何配置会话ID(Session IDs) 如何序列化会话(Session) 如何重定向或移动应用(Context) 如何让一个应用响应一个特定端口 使用JNDI 使用JNDI 在JNDI中配置数据源(DataSource) 内嵌Jetty服务器 内嵌Jetty教程 内嵌Jetty的HelloWorld教程 内嵌Jetty视频 优化Jetty 如何配置垃圾收集 如何配置以支持高负载 在Jetty中部署应用 部署管理器 部署绑定 热部署 Context提供者 如何部署web应用 webApp提供者 如何部署第三方产品 部署展开形式的web应用 使用Jetty进行开发 如何使用Jetty进行开发 如何编写Jetty中的Handlers 使用构建工具 如何在Maven中使用Jetty 如何在Ant中使用Jetty Maven和Ant的更多支持 Jetty Maven插件(Plugin) Jetty Jspc Maven插件(Plugin) Maven web应用工程原型 Ant Jetty插件(Plugin) 使用集成开发环境(IDEs) 在Eclipse中使用Jetty 在IntelliJ中使用Jetty 在Eclipse中工作 在Eclipse中开发Jetty Jetty WTP插件(Plugin) JettyOSGi SDK for Eclipse-PDE EclipseRT Jetty StarterKit SDK OSGi Jetty on OSGi, RFC66 基于Jetty OSGi的产品 OSGi贴士 Equinox中使用Jetty实现HTTP Service Felix中使用Jetty实现HTTP Service PAX中使用Jetty实现HTTP Srevice ProSyst mBedded Server Equinox Edition Spring Dynamic Modules里的Jetty JOnAS5里的Jetty 配置Ajax、Comet和异步Servlets 持续和异步Servlets 100 Continue和102 Processing WebSocket Servlet 异步的REST Stress Testing CometD 使用Servlets和Filters Jetty中绑定的Servlets Quality of Service Filter Cross Origin Filter 配置安全策略(Security Policies) 安全领域(Security Realms) 安全域配置教程 Java Authentication and Authorization Service (JAAS) JAAS配置教程 JASPI 安全模型(Secure Mode) 存储在文件中的安全密码以及编程教程 如何开启或禁止Jetty中的SSL功能 如何在Jetty中安全存储密码 如何安全终止Jetty 如何配置Spnego Application Server Integrations(集成) Apache Geronimo JEE 配置Apache httpd和Jetty教程 配置Apache mod_proxy和Jetty 配置Jetty中的AJP13 在JBoss中配置Jetty Remote Glassfish EJBs from Jetty Jetty and Spring EJB3 (Pitchfork) JBoss EJB3 ObjectWeb EasyBeans

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值