我正在嘗試使用 Jersey Library 構建一個 REST API。API 中的 Post 方法應該使用 JSON 正文。當我嘗試通過 Postman 發送請求時,我收到 405 錯誤。@POST@Path("CreateADGroup")@Produces(MediaType.APPLICATION_JSON)@Consumes("application/json")@RequiredRight(value = "createADGroup")public Map postCreateADGroup(RequestValues requestValues) { log.debug("POST CreateADGroup"); log.debug("This is the value" + requestValues.getValue()); // using a test map untill i can get the actual values. Map ret = new HashMap(); ret.put("Val", "TestVal"); return ret;}這是POJO方法public class RequestValues {String value;public String getValue() { return value;}public void setValue(String value) { this.value = value;}}我曾嘗試使用 Direct JsonObject 初始化,但我收到了同樣的錯誤。@POST@Path("CreateADGroup")@Produces(MediaType.APPLICATION_JSON)@Consumes("application/json")@RequiredRight(value = "createADGroup")public Map postCreateADGroup(JSONObject requestValues) {Map ret = new HashMap(); ret.put("Val", "TestVal"); return ret; }
1 回答

滄海一幻覺
TA貢獻1824條經驗 獲得超5個贊
是的,我想出了答案。我將方法簽名更改為 Map。
@POST
@Path("CreateADGroup")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/json")
@RequiredRight(value = "createADGroup")
public Map postCreateADGroup(Map requestValues) {
log.debug("POST CreateADGroup");
log.debug("This is the value" + requestValues.get("value"));
Map ret = new HashMap();
ret.put("Val", "No Val");
return ret;
}
添加回答
舉報
0/150
提交
取消