我对这里写的内容有一个相反的错误。
我只是想在 Eclipse 中运行一个非常简单的 Restlet 示例应用程序。
邮件服务器应用程序.java
public class MailServerApplication extends Application {
/**
* Launches the application with an HTTP server.
*
* @param args
* The arguments.
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Server mailServer = new Server(Protocol.HTTP, 8111);
mailServer.setNext(new MailServerApplication());
mailServer.start();
}
/**
* Constructor.
*/
public MailServerApplication() {
setName("RESTful Mail Server");
setDescription("Example for 'Restlet in Action' book");
setOwner("Restlet S.A.S.");
setAuthor("The Restlet Team");
}
/**
* Creates a root Router to dispatch call to server resources.
*/
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("http://localhost:8111/",
RootServerResource.class);
router.attach("http://localhost:8111/accounts/",
AccountsServerResource.class);
router.attach("http://localhost:8111/accounts/{accountId}",
AccountServerResource.class);
return router;
}
}
RootServerResource.java
public class RootServerResource
extends ServerResource implements RootResource {
public String represent() {
return "This is the root resource";
}
public String describe() {
throw new RuntimeException("Not yet implemented");
}
}
根资源.java
/**
* Root resource.
*/
public interface RootResource {
/**
* Represents the application root with a welcome message.
*
* @return The root representation.
*/
@Get("txt")
public String represent();
}
如果我在本地运行服务器并且在我的浏览器“localhost:8111”上键入包括本地主机在内的完整 uri,则该代码可以完美运行。但是,一旦我将路由器声明更改为路由器,页面总是会抛出 404 错误。
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/", RootServerResource.class);
router.attach("/accounts/", AccountsServerResource.class);
router.attach("/accounts/{accountId}", AccountServerResource.class);
return router;
}
换句话说,如果我将包含 http 和 ip 地址的完整路径附加到路由器,它可以正常工作,但相对路径不能。
这很奇怪。如果有任何错误,我会假设相对定义应该有效,而 localhost 定义不应该,但我遇到的情况恰恰相反。有什么建议么?
编辑:
根据要求,我包括我的 AccountServerResource.class
/**
* Implementation of a mail account resource.
*/
public class AccountServerResource extends ServerResource implements
AccountResource {
/** The account identifier. */
private int accountId;
/**
* Retrieve the account identifier based on the URI path variable
* "accountId" declared in the URI template attached to the application
* router.
*/
@Override
protected void doInit() throws ResourceException {
this.accountId = Integer.parseInt(getAttribute("accountId"));
}
public String represent() {
return AccountsServerResource.getAccounts().get(this.accountId);
}
public void store(String account) {
AccountsServerResource.getAccounts().set(this.accountId, account);
}
public void remove() {
AccountsServerResource.getAccounts().remove(this.accountId);
}
}
AccountResource 接口:
/**
* User account resource.
*/
public interface AccountResource {
/**
* Represents the account as a simple string with the owner name for now.
*
* @return The account representation.
*/
@Get("txt")
public String represent();
/**
* Stores the new value for the identified account.
*
* @param account
* The identified account.
*/
@Put("txt")
public void store(String account);
/**
* Deletes the identified account by setting its value to null.
*/
@Delete
public void remove();
}