質問

I want to develop REST services with Restlet. I have one class that extends the org.restlet.Application class, and other that extends the org.restlet.resource.ServerResource class.

The first class has one method:

public class ServicesApplication extends Application {      
    public Restlet createInboundRoot() {

        // Create a router Restlet that routes each call to a

        // new instance of HelloWorldResource.

        Router router = new Router(getContext());

        // Defines only one route

        router.attach("/myServices", Services.class);

        return router;

    }
}

And the second class has the different methods of the services, for example a HelloWorld:

public class Services extends ServerResource{
    @Get ("hello")
    public void Hello(String name){
        System.out.println("Hello "+name);
    }
}

The problem is that I don't know how can I deploy my services in Tomcat, do I need to generate a .war and store in the webapps folder of Tomcat??. And I don't know if in the class that implement the ServerResource I need to call to the class that implement to the Application class. Finally I don't know what is exactly the URL to call to "Hello": http://myDomain:myPort/myServices/hello?name=MyName ????

Thank in advance

UPDATE

I update my app: I change ServicesApplication and now it is an abstract class. Also I add other class that extends to the ServicesApplication class:

    public class HelloWorldService extends Services {

    public String Hello(String name){
        System.out.println("Hello "+name);
        return("Hello"+name);
    }
}

Of course, Hello is an abstract method of Services class.

Also I add in ServicesApplication this line:

router.attach("/helloWorld/{name}", HelloWorldService.class);

I'm calling to this method from my navigator:

http://localhost:8080/myServices/service/helloWorld/Jesus

and this is the response:

Hello null

How do I have to call to the method???

I only want to do a Hello +name. When I have this, I want to develop some GET and POST methods, with JSON String as parameters. But if it's very difficult to develop a simple HelloWorld with Reslet, maybe I'll try with RestEasy.

:__(

役に立ちましたか?

解決

Export your project as a .war file and deploy it on your server. Then your path will be http://localhost:port/Project_Name/myServices. If you want to pass a parameter you have to add router.attach("/myServices/{name}", Services.class);. And you can write http://localhost:port/Project_Name/myServices/myName.

Take a look at this example.

他のヒント

This page here should get you started and give you some ideas.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top