繁体   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