亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我向服務器發送帶有參數的 get 請求,我會收到 405 Method not allowed

如果我向服務器發送帶有參數的 get 請求,我會收到 405 Method not allowed

小怪獸愛吃肉 2023-03-23 15:41:25
如果我向我的服務器發送一個帶有參數的 GET 請求,我會得到一個 405 - Method not allowed. 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。我的郵遞員請求網址: http://localhost:8080/Server_war_exploded/user?userId=1我忘記了代碼中的某些內容嗎?
查看完整描述

3 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

通過在用戶 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) {

    }


查看完整回答
反對 回復 2023-03-23
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

使用這個@Path("/{userId}")。這會起作用。

網址會像這樣:http://localhost:8080/Server_war_exploded/user/{userId}

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


查看完整回答
反對 回復 2023-03-23
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

@Path("{userId}")

它缺少正則表達式。它如何知道現在的用戶 ID 是什么以及路徑的下一步是什么?

它應該是這樣的(如果你的 id 真的是一個字符串......): @Path("{userId:\\w+}")


查看完整回答
反對 回復 2023-03-23
  • 3 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號