我正在使用Eclipse开发一个CoAP应用程序,我需要像在restful服务中一样,使用该URL传递参数。是否有可能在口径coap实现,如果可以,请让我知道如何做。例:
coap://localhost:5683/foo/{fooID}发布于 2015-11-02 21:56:48
简单地说是的,你能做到。
如JavaDocs中所述
因此,基本上,您必须在org.eclipse.californium.core.server.ServerMessageDeliverer中重写org.eclipse.californium.core.server.ServerMessageDeliverer中的findResource方法,以便返回处理请求的适当资源。此外,还需要将Exchange作为资源handleGET/PUT/POST/etc的一部分进行分析,以获取路径变量(这可以通过使用UriPath实现)。
基于加利福尼亚的源代码,应该可以很容易地覆盖请求传递者的默认行为。
那就祝你好运!
发布于 2017-03-17 12:21:56
从我到目前为止看到的情况来看,创建一个自定义ServerMessageDeliverer似乎是更复杂的解决方案。实际上,正确的解决方案似乎是覆盖CoapResource#getChild(String),因此它返回您希望与该名称关联的资源。在我看来,ServerMessageDeliverer更像是在更复杂的环境中实现某种交付或分发请求的控制器的方式。
对于URI的最后一部分是参数的问题,解决方案可以如下所示:
public class UriParameterResource extends CoapResource {
public UriParameterResource() {
super("foo");
}
@Override
public void handleGET(CoapExchange exchange) {
List<String> uriPath = exchange.getRequestOptions().getUriPath();
// check if there is a sub-resource given, and if so use it for processing
if (uriPath.size() > 1) {
exchange.respond("Process " + uriPath.get(1));
} else {
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
}
@Override
public Resource getChild(String name) {
// even sub-resources will land in the GET of this resource
return this;
}
}关于@哥白尼克的答案,我个人认为它不符合休息的想法。URI路径的每个部分都应该返回与其父路径相关的自己的资源,这使得它成为每个定义的树结构,而不是一个平面列表,它只是作为某种参数检查路径的部分。
IMHO,即使是传感器示例,也可以通过使用CoapResource实现来解析,其中可以动态解析可变子资源。下面的片段只是一个例子,当然,这需要依赖于一个房子和它的房间需要注册的真实情况。
public class HousesResource extends CoapResource {
public HousesResource() {
super("houses");
}
@Override
public void handleGET(CoapExchange exchange) {
// could return a list of available houses
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
@Override
public Resource getChild(String name) {
Resource child = super.getChild(name);
if (child == null) {
child = new HouseResource(name);
add(child);
}
return child;
}
class HouseResource extends CoapResource {
public HouseResource(String name) {
super(name);
add(new RoomsResource());
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
}
class RoomsResource extends CoapResource {
public RoomsResource() {
super("rooms");
}
@Override
public void handleGET(CoapExchange exchange) {
// could return a list of available rooms
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
@Override
public Resource getChild(String name) {
Resource child = super.getChild(name);
if (child == null) {
child = new RoomResource(name);
add(child);
}
return child;
}
}
class RoomResource extends CoapResource {
public RoomResource(String roomName) {
super(roomName);
add(new SensorsResource());
}
@Override
public void handleGET(CoapExchange exchange) {
// could return a summary board about the room
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
}
class SensorsResource extends CoapResource {
public SensorsResource() {
super("sensors");
add(new TemperatureResource());
}
@Override
public void handleGET(CoapExchange exchange) {
// this could return a list of registered sensors
exchange.respond(ResponseCode.NOT_IMPLEMENTED);
}
}
class TemperatureResource extends CoapResource {
public TemperatureResource() {
super("temperature");
}
@Override
public void handleGET(CoapExchange exchange) {
// as the structure is fixed we know that two levels up
// there is the room, and four levels up there is the house
String room = getParent().getParent().getName();
String house = getParent().getParent().getParent().getParent().getName();
exchange.respond("The temperature of the " + house + " in the " + room + " is : 25 degree");
}
}
}在该示例中,如果资源以前不存在,则动态创建它们。这也可以通过一些查找或注册机制来交换(例如,通过PUT或PUSH方式注册房屋)。
别误会我的意思。@哥白克的解决方案似乎有效,并且可能是一些场景的合适解决方案(例如,每个家庭都有自己的服务器,请求需要重定向),但对于一个相当简单的场景,我认为这不是正确的方法。
https://stackoverflow.com/questions/31387805
复制相似问题