[英]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.