Restlet path param does not work
Asked Answered
D

1

8

Below is my routing

public Restlet createInboundRoot(){
 Router router = new Router(getContext());
router.attach("account/profile",UserProfile.class);

Following is the Resource 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

I want to invoke one resource class and do couple of identical functions for a resource class. That means, my above resource class handles functions related to the UserProfiles such as add, modify. URL are:
account/profile/add => to add a user
account/profile/modify => to modify a user

anyway, above my implementation doesn't work as only the test() method can be invoked through account/profile/

I tried with Pathparams as well.But it also didnot work. For path params:

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

was added and in the resource class,

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

Anyone tell me where is my problem.

Decaliter answered 13/1, 2016 at 12:49 Comment(6)
Can you post your err log here?Junkman
Thanks karthi for attention. There is no errors thrown.. Server just returns 403 as responseEosinophil
Well! It means the resource you are trying to access is exist but the server unable to give a proper response. You can try these things, make sure you directory has all permissions and specify a produce type either json or XML by using @ Produces annotation and give a try with @put or @ get methods. Sometimes post is culprit.Junkman
Thanks Karthi.. I tried with single method annotated @POST with single ULR to the source and then it worked. I don't see any permission issue. This problem rises when 2 URLs calls this resource.Eosinophil
Any has any thought please ?Eosinophil
as a side note, don't use action names in the path of your resources. a resource is more about state, note action. If you want action, use the HTTP verbs.Alcoran
K
0

The way you attach your UserProfile server resource is a bit strange. I think that you mixed the native routing of Restlet and the one from the JAXRS extension.

I made some tests regarding your use case and I was able to have the behavior you expect. I used the version 2.3.5 of Restlet.

Here is what I did:

  • Since you want to use JAXRS, you need to create a JaxRsApplication and attach it on the component:

    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();
    
  • The application simply list the server resources you want to use but doesn't define routing and paths:

    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;
        }
    }
    
  • The server resource defines handling methods and associated routes:

    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");
        }
    }
    
  • When I call the different paths, right methods are called:

    • http://localhost:8182/account/profile/modify: the modifyUser method is called
    • http://localhost:8182/account/profile/add: the addUser method is called
    • http://localhost:8182/account/profile/: the test method is called

Hope it helps you, Thierry

Killam answered 20/1, 2016 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.