【问题标题】:Restlet path param does not workRestlet 路径参数不起作用
【发布时间】:2016-04-18 10:58:17
【问题描述】:

下面是我的路线

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 相关的功能,例如添加、修改。 网址是:
account/profile/add => 添加用户
account/profile/modify => 修改用户

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

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

router.attach("account/profile/{action}",UserProfile.class);

已添加并在资源类中,

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

谁能告诉我我的问题出在哪里。

【问题讨论】:

  • 你能把你的错误日志贴在这里吗?
  • 感谢 karthi 的关注。没有抛出任何错误。服务器只返回 403 作为响应
  • 好吧!这意味着您尝试访问的资源存在,但服务器无法给出正确的响应。您可以尝试这些事情,确保您的目录具有所有权限,并使用 @Produces 注释指定生成类型为 json 或 XML,并尝试使用 @put 或 @get 方法。有时帖子是罪魁祸首。
  • 谢谢Karthi .. 我尝试使用单个方法注释@POST 和单个ULR 到源,然后它工作。我没有看到任何权限问题。当 2 个 URL 调用此资源时,就会出现此问题。
  • 请问有什么想法吗?

标签: java jakarta-ee jax-rs restlet


【解决方案1】:

附加UserProfile 服务器资源的方式有点奇怪。我认为你混合了 Restlet 的本地路由和来自 JAXRS 扩展的路由。

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

这是我所做的:

  • 由于要使用 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方法

希望对你有帮助, 蒂埃里

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2013-07-06
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多