java - 如果我向服务器发送带有参数的 get 请求,我会收到 405 Method not allowed by Postman

标签 java http server glassfish

如果我向服务器发送带有参数的 GET 请求,我会收到 405 - 方法不允许

 package pkgService;

 import com.fasterxml.jackson.databind.ObjectMapper;
 import pkgData.pkgEmployee.User;
 import pkgServer.pkgUser.UserManagement;

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

@Path("/user")
public class UserRouter {

     private UserManagement userManagement;
     private ObjectMapper objMap;

     public UserRouter() {
         this.userManagement = new UserManagement();
         objMap = new ObjectMapper();

         //TODO delete test data
         userManagement.addUser(new User(1,"lukad", "luki"));
         userManagement.addUser(new User(2,"meli", "malal"));
     }

     @GET
     @Path("{userId}")
     public Response getBook(@PathParam("userId") String id) {
         Response.ResponseBuilder response = Response.status(Response.Status.OK);
         try {
             response.entity(objMap.writeValueAsString(userManagement.getUser(id)));
         } catch (Exception e) {
             response.status(Response.Status.BAD_REQUEST);
             response.entity("[ERROR] " + e.getMessage());
         }
         return response.build();
     } }

我希望获得 id 为 1 (lukad,luki) 的用户,但我收到了 405。

我的 postman 请求网址: http://localhost:8080/Server_war_exploded/user?userId=1

我是否忘记了代码中的某些内容?

最佳答案

通过在用户 ID 前添加“/”来更改功能

@GET
@Path("/{userId}")
     public Response getBook(@PathParam("userId") String id) {
}

此外,如果您使用 PathParam,那么您还需要将 url 更改为

 http://localhost:8080/Server_war_exploded/user/1

其中 1 是用户 ID

但是如果你想使用

 http://localhost:8080/Server_war_exploded/user?userId=1

然后您需要使用 QueryParams 并按如下方式更改代码

 @GET
 public Response getBook(@QueryParam("userId") String id) {
    }

关于java - 如果我向服务器发送带有参数的 get 请求,我会收到 405 Method not allowed by Postman,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57004352/

相关文章:

带有发布数据的 php file_get_contents

http - 当资源可用但由于权限而无法访问时更正 HTTP 状态代码

delphi - 将 Indy 与 Tor 结合使用

c# - 如何检测图像上的水位?

java - Android自定义图库禁用滚动

java - 如何以最少的内存运行 spring boot fat jar?

java - 如何将 Web 服务器指向我的 Web 应用程序?

java - tika-app-1.7.jar 与 tika-server-1.7.jar

python - 如何创建 Python API 并将其与 React Native 一起使用?

java - 这些页面包括带参数的编写函数