Java Restlet路径参数不起作用

Java Restlet路径参数不起作用,java,jakarta-ee,jax-rs,restlet,Java,Jakarta Ee,Jax Rs,Restlet,下面是我的路线 public Restlet createInboundRoot(){ Router router = new Router(getContext()); router.attach("account/profile",UserProfile.class); 以下是资源类UserProfile.java @post @path("add") public void addUser(User user){ @post @path("modify") public void m

下面是我的路线

public Restlet createInboundRoot(){
 Router router = new Router(getContext());
router.attach("account/profile",UserProfile.class);
以下是资源类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相关的函数,如add、modify。 URL为:
account/profile/add=>添加用户
account/profile/modify=>修改用户

无论如何,上面我的实现不起作用,因为只有test()方法可以通过account/profile/调用

我也尝试过Pathparams,但也不起作用。 对于路径参数:

router.attach("account/profile/{action}",UserProfile.class);
已添加并在资源类中

@post
@path("{action}")
public void addUser(@pathparam("action") String action, User user){ 

有人告诉我哪里出了问题。

您附加
用户配置文件的方式有点奇怪。我认为您混合了Restlet的本机路由和来自JAXRS扩展的路由

我对您的用例进行了一些测试,我能够获得您期望的行为。我使用了Restlet的2.3.5版本

以下是我所做的:

  • 由于要使用JAXRS,您需要创建一个
    JAXR应用程序
    ,并将其附加到组件上:

    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;
        }
    }
    
  • 当我调用不同的路径时,会调用正确的方法:

    • http://localhost:8182/account/profile/modify
      :调用
      modifyUser
      方法
    • http://localhost:8182/account/profile/add
      :调用
      addUser
      方法
    • http://localhost:8182/account/profile/
      :调用
      测试
      方法
希望它能帮助你,
蒂埃里

你能在这里发布你的错误日志吗?谢谢卡蒂的关注。没有抛出错误。。服务器只返回403作为responseWell!这意味着您试图访问的资源存在,但服务器无法给出正确的响应。您可以尝试这些方法,确保您的目录具有所有权限,并通过使用@products注释指定一个product类型json或XML,并尝试使用@put或@get方法。有时波斯特是罪魁祸首。谢谢卡蒂。。我尝试了使用单个方法注释@POST,并将单个ULR添加到源代码,然后成功了。我看不到任何许可问题。当2个URL调用此资源时,会出现此问题。有人有任何想法吗?
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");
    }
}