我正在學習 JAX-RS,作為一項活動,我創建了一個端點,它將列出 DB 中的所有 TODO。
@Path("list")
@GET
public List<Todo> getTodos(){
return todoService.getTodos();
}
終點網址是
http://localhost:9090/hello-todo/api/v1/todo/list
目前它列出了所有的 TODO 項,因為我正在學習查詢引數,所以我決定添加一個條件并僅列出已完成的 TODO 項。
終點網址是
http://localhost:9090/hello-todo/api/v1/todo/list?taskstatus=completed
我寫的實作方法是@Path("list") @GET public List getFilteredTodos(@QueryParam("taskstatus") String taskstatus_){ return todoService.getFilteredTodos(); }
構建很好,但是在部署時會引發以下錯誤:
The application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.util.List academy.learnprogramming.rest.TodoRest.getTodos() and public java.util.List academy.learnprogramming.rest.TodoRest.getFilteredTodos(java.lang.String) at matching regular expression /list. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@8bd9d08']
我知道部署失敗,因為兩者都具有相同的 URI,但我該如何處理這種情況?
可能的解決方案:
@Path("list")
@GET
public List<Todo> getFilteredTodos(@QueryParam("taskstatus") String taskstatus_){
if (taskstatus_!=null && !taskstatus_.isEmpty()) {
return todoService.getFilteredTodos(taskstatus_);
}else{
return todoService.getTodos();
}
}
uj5u.com熱心網友回復:
可能的解決方案:使用@Path("list") 實作一種方法,并使用內部的空taskstatus 引數值處理這種情況
uj5u.com熱心網友回復:
您可以創建如下內容:
@GET
@Path("list/{taskStatus}")
public List getFilteredTodos(@PathParm("taskStatus") String taskStatus) {
// do the filtering
}
因此,您將擁有一條不會與主要路徑沖突的不同路徑。
更多細節在這里和這里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/408237.html
標籤:
