在Restlet中的HTTP方法内创建客户端请求

时间:2014-09-16 09:27:07

标签: java httprequest 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;
}

}

这样,当客户端在items资源上发送POST请求时,不仅会创建一个Item,还会创建一个订单。

我在我的代码中尝试了它,但它导致了错误:

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);)放在main方法中,它就可以了。如果我将它们放在HTTP请求中,它就不起作用。

所以我正在寻找一种在HTTP方法中发送客户端请求的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要在web.xml中的servlet声明中添加以下init参数。有了这个,您的应用程序将能够进行HTTP HTTPS和FILE协议调用。

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