[英]Restlet path param does not work
以下是我的路線
public Restlet createInboundRoot(){
Router router = new Router(getContext());
router.attach("account/profile",UserProfile.class);
以下是Resource類UserProfile.java
@post
@path("add")
public void addUser(User user){
@post
@path("modify")
public void modifyUser(User user){
@post
public void test(){//only this is called
我想調用一個資源類,並為一個資源類做幾個相同的函數。 這意味着,我上面的資源類處理與UserProfiles相關的功能,例如添加,修改。 網址是:
帳戶/個人資料/添加=>添加用戶
account / profile / modify =>修改用戶
無論如何,以上我的實現均無效,因為只能通過帳戶/配置文件/調用test()方法
我也嘗試過Pathparams,但是也沒有用。 對於路徑參數:
router.attach("account/profile/{action}",UserProfile.class);
已添加,並且在資源類中,
@post
@path("{action}")
public void addUser(@pathparam("action") String action, User user){
有人告訴我我的問題在哪里。
附加UserProfile
服務器資源的方式有些奇怪。 我認為您混合了Restlet的本機路由和JAXRS擴展中的本機路由。
我對您的用例進行了一些測試,並且能夠實現您期望的行為。 我使用Restlet的2.3.5版本。
這是我所做的:
由於要使用JAXRS,因此需要創建一個JaxRsApplication
並將其附加到組件上:
Component component = new Component(); component.getServers().add(Protocol.HTTP, 8182); // JAXRS application JaxRsApplication application = new JaxRsApplication(component.getContext()); application.add(new MyApplication()); // Attachment component.getDefaultHost().attachDefault(application); // Start component.start();
該應用程序僅列出您要使用的服務器資源,但沒有定義路由和路徑:
import javax.ws.rs.core.Application; public class MyApplication extends Application { public Set<Class<?>> getClasses() { Set<Class<?>> rrcs = new HashSet<Class<?>>(); rrcs.add(AccountProfileServerResource.class); return rrcs; } }
服務器資源定義處理方法和關聯的路由:
import javax.ws.rs.POST; import javax.ws.rs.Path; @Path("account/profile/") public class AccountProfileServerResource { @POST @Path("add") public User addUser(User user) { System.out.println(">> addUser"); return user; } @POST @Path("modify") public User modifyUser(User user) { System.out.println(">> modifyUser"); return user; } @POST public void test() { System.out.println(">> test"); } }
當我調用不同的路徑時,將調用正確的方法:
http://localhost:8182/account/profile/modify
:調用modifyUser
方法 http://localhost:8182/account/profile/add
:調用addUser
方法 http://localhost:8182/account/profile/
:稱為test
方法 希望對您有幫助,蒂埃里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.