gpt4 book ai didi

java - Swagger UI 仅显示获取端点。

转载 作者:行者123 更新时间:2023-12-02 03:41:31 24 4
gpt4 key购买 nike

我有几个休息端点,我想通过 Swagger 来公开它们。我已经下载了 swagger rest 2 的示例。当我在 swagger ui 中查看生成的 json 时,它仅显示获取端点。未显示 Post 和 Put 端点。请帮忙。

这是我的 web.xml

   <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
io.swagger.jaxrs.listing,
io.swagger.sample.resource
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.wadl.disableWadl</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</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:8080/SwaggerWithRest2/api</param-value>
</init-param>
<init-param>
<param-name>swagger.filter</param-name>
<param-value>io.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<filter>
<filter-name>ApiOriginFilter</filter-name>
<filter-class>io.swagger.sample.util.ApiOriginFilter</filter-class>
</filter>
<servlet>
<servlet-name>Bootstrap</servlet-name>
<servlet-class>io.swagger.sample.Bootstrap</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<filter-mapping>
<filter-name>ApiOriginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

这是我的终点类(class)

/**
* Copyright 2016 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.swagger.sample.resource;

import io.swagger.annotations.*;
import io.swagger.sample.data.UserData;
import io.swagger.sample.model.User;
import io.swagger.sample.exception.ApiException;

import javax.ws.rs.core.Response;
import javax.ws.rs.*;

@Path("/user")
@Api(value="/user", description = "Operations about user")
@Produces({"application/json", "application/xml"})
public class UserResource {
static UserData userData = new UserData();

@POST
@ApiOperation(value = "Create user",
notes = "This can only be done by the logged in user.",
position = 1)
public Response createUser(
@ApiParam(value = "Created user object", required = true) User user) {
userData.addUser(user);
return Response.ok().entity("").build();
}

@POST
@Path("/createWithArray")
@ApiOperation(value = "Creates list of users with given input array",
position = 2)
public Response createUsersWithArrayInput(@ApiParam(value = "List of user object", required = true) User[] users) {
for (User user : users) {
userData.addUser(user);
}
return Response.ok().entity("").build();
}

@POST
@Path("/createWithList")
@ApiOperation(value = "Creates list of users with given input array",
position = 3)
public Response createUsersWithListInput(@ApiParam(value = "List of user object", required = true) java.util.List<User> users) {
for (User user : users) {
userData.addUser(user);
}
return Response.ok().entity("").build();
}

@PUT
@Path("/{username}")
@ApiOperation(value = "Updated user",
notes = "This can only be done by the logged in user.",
position = 4)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid user supplied"),
@ApiResponse(code = 404, message = "User not found") })
public Response updateUser(
@ApiParam(value = "name that need to be deleted", required = true) @PathParam("username") String username,
@ApiParam(value = "Updated user object", required = true) User user) {
userData.addUser(user);
return Response.ok().entity("").build();
}

@DELETE
@Path("/{username}")
@ApiOperation(value = "Delete user",
notes = "This can only be done by the logged in user.",
position = 5)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid username supplied"),
@ApiResponse(code = 404, message = "User not found") })
public Response deleteUser(
@ApiParam(value = "The name that needs to be deleted", required = true) @PathParam("username") String username) {
if (userData.removeUser(username)) {
return Response.ok().entity("").build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

@GET
@Path("/{username}")
@ApiOperation(value = "Get user by user name",
response = User.class,
position = 0)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid username supplied"),
@ApiResponse(code = 404, message = "User not found") })
public Response getUserByName(
@ApiParam(value = "The name that needs to be fetched. Use user1 for testing. ", required = true) @PathParam("username") String username)
throws ApiException {
User user = userData.findUserByName(username);
if (null != user) {
return Response.ok().entity(user).build();
} else {
throw new io.swagger.sample.exception.NotFoundException(404, "User not found");
}
}

@GET
@Path("/login")
@ApiOperation(value = "Logs user into the system",
response = String.class,
position = 6)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid username/password supplied") })
public Response loginUser(
@ApiParam(value = "The user name for login", required = true) @QueryParam("username") String username,
@ApiParam(value = "The password for login in clear text", required = true) @QueryParam("password") String password) {
return Response.ok()
.entity("logged in user session:" + System.currentTimeMillis())
.build();
}

@GET
@Path("/logout")
@ApiOperation(value = "Logs out current logged in user session",
position = 7)
public Response logoutUser() {
return Response.ok().entity("").build();
}
}

这是我的 jar

enter image description here

enter image description here

最佳答案

这可能与web.xmlJersey2Config中的ApiAuthorizationFilterImpl有关。

该类的注释指出:

/**
* The rules are maintained in simple map with key as path and a boolean value
* indicating given path is secure or not. For method level security the key is
* combination of http method and path.
*
* If the resource or method is secure then it can only be viewed using a
* secured api key
*
* Note: Objective of this class is not to provide fully functional
* implementation of authorization filter. This is only a sample demonstration
* how API authorization filter works.
*
*/

并且方法isOperationAllowed声明所有非get或者路径以'store开头的方法都需要检查key是否可用。 (快速推导,可能有错)。

因此,如果您删除过滤器,它可能会起作用。

关于java - Swagger UI 仅显示获取端点。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36786821/

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