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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值