我想用ReSTLet开发REST服务。我有一个类扩展 org.reSTLet.Application 类,另一个类扩展 org.reSTLet.resource.ServerResource 类。
第一个类有一个方法:
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;
}
}
第二个类具有不同的服务方法,例如HelloWorld:
public class Services extends ServerResource{
@Get ("hello")
public void Hello(String name){
System.out.println("Hello "+name);
}
}
问题是我不知道如何在Tomcat中部署我的服务,我是否需要生成.war并存储在Tomcat的webapps文件夹中?我不知道在实现 ServerResource 的类中是否需要调用实现 Application 类的类。最后我不知道调用“Hello”的 URL 到底是什么: http://myDomain:myPort/myServices/hello?name=MyName
????
提前致谢
更新
我更新了我的应用程序:我更改了 ServicesApplication,现在它是一个抽象类。我还添加了扩展至 ServicesApplication 类的其他类:
public class HelloWorldService extends Services {
public String Hello(String name){
System.out.println("Hello "+name);
return("Hello"+name);
}
}
当然,Hello
是Services
类的抽象方法。
我还在 ServicesApplication
中添加了这一行:
router.attach("/helloWorld/{name}", HelloWorldService.class);
我从导航器调用此方法:
http://localhost:8080/myServices/service/helloWorld/Jesus
这是响应:
Hello null
我该如何调用该方法???
我只想打个Hello +name
。当我有了这个之后,我想开发一些 GET 和 POST 方法,以 JSON 字符串作为参数。但如果用 Reslet 开发一个简单的 HelloWorld 非常困难,也许我会尝试使用 RestEasy。
:__(
最佳答案
将您的项目导出为.war 文件并将其部署到您的服务器上。那么您的路径将为http://localhost:port/Project_Name/myServices
。如果您想传递参数,则必须添加router.attach("/myServices/{name}", Services.class);
。您可以编写http://localhost:port/Project_Name/myServices/myName
。
看看this example .
关于java - 开发和部署 ReSTLet 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698839/