gpt4 book ai didi

java - Swagger 配置 - 访问 swagger.json 时出现 404

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:50 24 4
gpt4 key购买 nike

我是 swagger 新手,希望在我最新的 REST API 项目上设置它,但失败得很惨。一切似乎都配置正确,但我无法在没有收到 404 的情况下导航到 swagger.json。我看不到缺少或错误的内容

pom.xml

    <dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.0</version>

<exclusions>
<exclusion>
<artifactId>jersey-server</artifactId>
<groupId>com.sun.jersey</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-multipart</artifactId>
<groupId>com.sun.jersey.contribs</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.11.1</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11.1</version>
</dependency>

排除,因为项目使用版本 1.11.1,如果从 swagger 导入 1.13 会导致冲突

web.xml

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
io.swagger.jaxrs.json,
io.swagger.jaxrs.listing,
uk.package.test.myproject
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8082/myproject/api</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

注释示例...

@ManagedBean
@Path("secure/cache")
@Restrict("ManageCache")
@Api(value = "secure/cache", description = "Rest api for do operations on admin", produces = MediaType.APPLICATION_JSON)
public class AdminApi {....


@GET
@Path("{name}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Get specific admin", httpMethod = "GET", notes = "Fetch the admin user details", response = Response.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Given admin user found"),
@ApiResponse(code = 404, message = "Given admin user not found"),
@ApiResponse(code = 500, message = "Internal server error due to encoding the data"),
@ApiResponse(code = 400, message = "Bad request due to decoding the data"),
@ApiResponse(code = 412, message = "Pre condition failed due to required data not found") })

public List<String> getCacheKeys(.....

当项目运行时,会记录以下内容...

10:46:48,724 INFO  [com.sun.jersey.api.core.PackagesResourceConfig] (MSC service thread 1-5) Scanning for root resource and provider classes in the packages:
io.swagger.jaxrs.json
io.swagger.jaxrs.listing
uk.package.test.myproject
10:46:48,771 INFO [com.sun.jersey.api.core.ScanningResourceConfig] (MSC service thread 1-5) Root resource classes found:
class uk.package.test.myproject.api.NotificationApi
class uk.package.test.myproject.api.UserApi
class uk.package.test.myproject.api.FlagsApi
class uk.package.test.myproject.api.UnitsByFunctionApi
class uk.package.test.myproject.api.CvDataApi
class uk.package.test.myproject.api.ImageApi
class uk.package.test.myproject.api.GazetteerApi
class uk.package.test.myproject.api.SearchApi
class uk.package.test.myproject.api.ViewApi
class uk.package.test.myproject.api.AdminApi
10:46:48,776 INFO [com.sun.jersey.api.core.ScanningResourceConfig] (MSC service thread 1-5) No provider classes found.
10:46:48,834 INFO [com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer] (MSC service thread 1-5) CDI support is enabled
10:46:48,842 INFO [com.sun.jersey.server.impl.application.WebApplicationImpl] (MSC service thread 1-5) Initiating Jersey application, version 'Jersey: 1.11.1 03/31/2012 06:49 PM'

提前谢谢

干杯

马丁

最佳答案

只是为了让您知道我已经通过使用自定义应用程序而不是使用包扫描解决了这个问题:)

感谢您的帮助

马丁

关于java - Swagger 配置 - 访问 swagger.json 时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com