ここに書かれていることとは逆のエラーが発生しています。
Eclipse で Restlet の非常に単純なサンプル アプリケーションを実行しようとしています。
MailServerApplication.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();
}
サーバーをローカルで実行している場合、およびブラウザーでローカルホストを含む完全な uri を「localhost:8111」と入力すると、コードはそのまま完全に機能します。ただし、ルーター宣言をルーターに変更するとすぐに、ページは常に 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 アドレスを含むフル パスをルーターにアタッチすると、正しく機能しますが、相対パスは機能しません。
これはかなり奇妙です。エラーが発生した場合、相対定義は機能し、ローカルホストの定義は機能しないと想定していましたが、私が経験しているのは正反対です。助言がありますか?
編集:
リクエストに応じて、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();
}