首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用JdkHttpServerFactory配置Swagger UI + Jersey 2

使用JdkHttpServerFactory配置Swagger UI + Jersey 2

JdkHttpServerFactory是一个用于配置和启动Java HTTP服务器的工具类。Swagger UI是一个用于可视化和交互式地展示API文档的工具。Jersey 2是一个用于构建RESTful Web服务的框架。

首先,我们需要导入JdkHttpServerFactory、Swagger UI和Jersey 2的相关依赖包,并添加到项目中。接下来,我们可以按照以下步骤配置Swagger UI + Jersey 2:

  1. 创建一个Jersey 2的Application类,该类继承自ResourceConfig,用于注册我们的资源类和配置Swagger。
代码语言:txt
复制
import io.swagger.jaxrs.config.BeanConfig;
import org.glassfish.jersey.server.ResourceConfig;

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        // 注册我们的资源类
        packages("com.example.resources");

        // 配置Swagger
        configureSwagger();
    }

    private void configureSwagger() {
        // 设置Swagger扫描的包路径
        String resourcePackage = "com.example.resources";
        packages(resourcePackage);

        // 设置Swagger UI的访问路径
        String swaggerPath = "/swagger";

        // 创建并配置Swagger的BeanConfig对象
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("/");
        beanConfig.setResourcePackage(resourcePackage);
        beanConfig.setScan(true);

        // 注册Swagger的资源类
        register(io.swagger.jaxrs.listing.ApiListingResource.class);
        register(io.swagger.jaxrs.listing.SwaggerSerializers.class);

        // 将Swagger UI的访问路径和Swagger的资源类关联起来
        register(io.swagger.jersey.config.JerseyJaxrsConfig.class);
        property("swagger.api.basepath", swaggerPath);
    }
}
  1. 创建一个资源类,该类包含我们需要暴露的RESTful API接口。
代码语言:txt
复制
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/example")
@Api(value = "Example API")
public class ExampleResource {

    @GET
    @ApiOperation(value = "Get example", notes = "Returns an example response")
    @Produces(MediaType.TEXT_PLAIN)
    public String getExample() {
        return "Example response";
    }
}
  1. 创建一个启动类,用于启动HTTP服务器并配置Jersey。
代码语言:txt
复制
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;

public class Main {
    public static void main(String[] args) {
        URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();

        // 创建Jersey的Application对象
        ResourceConfig config = new MyApplication();

        // 启动HTTP服务器
        JdkHttpServerFactory.createHttpServer(baseUri, config);
    }
}
  1. 在浏览器中访问Swagger UI。

Swagger UI的访问路径为:http://localhost:8080/swagger

这样,我们就配置好了Swagger UI + Jersey 2,并且可以通过Swagger UI来查看和测试我们的API接口。

推荐的腾讯云相关产品:

  • 腾讯云API网关:https://cloud.tencent.com/product/apigateway
  • 腾讯云云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云云数据库MySQL版:https://cloud.tencent.com/product/cdb_mysql
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云区块链服务:https://cloud.tencent.com/product/baas

请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券