簡體   English   中英

在Restlet中的HTTP方法內創建客戶端請求

[英]Create a Client request inside a HTTP method in Restlet

有沒有一種方法可以在HTTP方法內發送客戶端請求?

例如,我有Items資源,並且它具有POST方法

@Post

public Representation acceptItem(Representation entity) {

    1. get information from entity

    2. create new Item

    3. setStatus(Status.SUCCESS_CREATED)

    4. send POST request of another resource (e.g. orders resource)
    ClientResource ordersResource = new ClientResource(ordersURI);
    orderResource.post(info);
}

碼:

公共類ItemsResource擴展了BaseResource {

/**
 * Handle POST requests: create a new item.
 */
@Post
public Representation acceptItem(Representation entity) {
    Representation result = null;
    // Parse the given representation and retrieve pairs of
    // "name=value" tokens.
    Form form = new Form(entity);
    String itemName = form.getFirstValue("name");
    String itemDescription = form.getFirstValue("description");

    // Register the new item if one is not already registered.
    if (!getItems().containsKey(itemName)
            && getItems().putIfAbsent(itemName,
                    new Item(itemName, itemDescription)) == null) {
        // Set the response's status and entity
        setStatus(Status.SUCCESS_CREATED);
        Representation rep = new StringRepresentation("Item created",
                MediaType.TEXT_PLAIN);
        // Indicates where is located the new resource.
        rep.setLocationRef(getRequest().getResourceRef().getIdentifier() + "/"
                + itemName);
        result = rep;           

        //**In this POST method, send another POST request on another resource**
        ClientResource ordersResource = new ClientResource(
                "http://localhost:8111/firstResource/orders");
        ClientResource orderResource = null;

        // Create a new item
        Order order = new Order("order1", "this is an order.");
        try {
            Representation rO = ordersResource.post(getRepresentation(order));
            orderResource = new ClientResource(rO.getLocationRef());
        } catch (ResourceException e) {
            System.out.println("Error  status: " + e.getStatus());
            System.out.println("Error message: " + e.getMessage());
        }
    } else { // Item is already registered.
        setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        result = generateErrorRepresentation("Item " + itemName
                + " already exists.", "1");
    }

    return result;
}

}

因此,以這種方式,當客戶在項目資源上發送POST請求時,他不僅會創建項目,還會創建訂單。

我在我的代碼中嘗試過,但是導致錯誤:

Sep 16, 2014 6:57:49 PM org.restlet.engine.component.ClientRouter getNext
WARNING: The protocol used by this request is not declared in the list of client connectors. (HTTP/1.1). In case you are using an instance of the Component class, check its "clients" property.
Not Found (404) - The server has not found anything matching the request URI

如果我在主方法中放置這兩行( ClientResource ordersResource = new ClientResource(ordersURI); orderResource.post(info); ),它將起作用。 如果我將它們放入HTTP請求中,它將無法正常工作。

因此,我正在尋找一種以HTTP方法發送客戶端請求的方法?

您需要在web.xml的Servlet declation中添加以下init參數。 有了這個,您的應用程序將能夠進行HTTP HTTPS和FILE協議調用。

        <init-param>
            <param-name>org.restlet.clients</param-name>
            <param-value>HTTP HTTPS FILE</param-value>
        </init-param>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM