[英]Create a Client request inside a HTTP method in Restlet
Is there a way to send a client request inside a HTTP method? 有没有一种方法可以在HTTP方法内发送客户端请求?
For example, I have Items resource and it has POST method 例如,我有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);
}
code: 码:
public class ItemsResource extends BaseResource { 公共类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;
}
} }
So in this way, when the client send a POST request on items resource, not only he creates an Item, but also creates an order. 因此,以这种方式,当客户在项目资源上发送POST请求时,他不仅会创建项目,还会创建订单。
I tried it in my code but it caused error: 我在我的代码中尝试过,但是导致错误:
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
If I put these two lines( ClientResource ordersResource = new ClientResource(ordersURI); orderResource.post(info);
) in the main method, it works. 如果我在主方法中放置这两行( ClientResource ordersResource = new ClientResource(ordersURI); orderResource.post(info);
),它将起作用。 If I put them in a HTTP request, it doesn't work. 如果我将它们放入HTTP请求中,它将无法正常工作。
So I'm looking for a way to send a client request in a HTTP method? 因此,我正在寻找一种以HTTP方法发送客户端请求的方法?
You need to add the below init params inside the servlet declation in the web.xml . 您需要在web.xml的Servlet declation中添加以下init参数。 With this your application will be able to make HTTP HTTPS and FILE protocol calls. 有了这个,您的应用程序将能够进行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.