PDA

View Full Version : Building RESTful webservice using qxt



MichaII
9th August 2011, 10:09
Hi there,

I want to build a RESTful webservice. I could use some advice on my draft.
I found an example on how to build a soap-webservice here (https://sites.google.com/a/embeddedlab.org/community/technical-articles/qt/qt-posts/howtocreateasimplewebserviceusingqtandlibqxt) and used the structure of it.

Here's the interface of an resource:


#include <QString>

#include <QxtWebServiceDirectory>
#include <QxtWebRequestEvent>

class IResource : public QxtWebServiceDirectory
{
Q_OBJECT

public:
IResource(QString name, QxtAbstractWebSessionManager* sm, QObject* parent = 0):
QxtWebServiceDirectory(sm, parent),
name(name)
{
}

virtual ~IResource()
{
}

protected:
void indexRequested(QxtWebRequestEvent *event)
{
if (event->method.compare("GET") == 0)
{
doGET(event);
}
else if (event->method.compare("PUT") == 0)
{
doPUT(event);
}
else if (event->method.compare("POST") == 0)
{
doPOST(event);
}
else if (event->method.compare("DELETE") == 0)
{
doDELETE(event);
}
else
{
// not implemented - status code: 501
}
}

private:
void doGET(QxtWebRequestEvent *event)
{
// for now just say your name
postEvent(new QxtWebPageEvent(event->sessionID, event->requestID, name.toAscii()));
}

void doPUT(QxtWebRequestEvent *event)
{
}

void doPOST(QxtWebRequestEvent *event)
{
}

void doDELETE(QxtWebRequestEvent *event)
{
}

QString name;
};

Each resource should implement the Http-methods (to do something useful)
A session manager or as I called it a "dispatcher" handles the requests (sorry about the naming confusion):


#include <QxtHttpSessionManager>

#include "IResource.h"

class WebserviceDispatcher : public QxtHttpSessionManager
{
Q_OBJECT

public:
WebserviceDispatcher(const QHostAddress &host , quint16 port)
{
QxtHttpSessionManager(this);
rootService = new IResource("root", this, this);
setPort(port);
setListenInterface(host);
setConnector(HttpServer);
setStaticContentService(rootService);

qDebug() << "WebServer Started!";
}

virtual ~WebserviceDispatcher()
{
delete this->rootService;
}

void addService(QString uri, IResource* newService)
{
rootService->addService(uri, newService);
}

protected:
void incomingRequest(quint32 requestID, const QHttpRequestHeader & header, QxtWebContent *deviceContent)
{
QxtHttpSessionManager::incomingRequest(requestID, header, deviceContent);
qDebug() << "Request: "<< requestID;
qDebug() << "Method: " << header.method();
qDebug() << "URI: " << header.path();
}

private:
IResource* rootService;
};


finally main looks like this:



#include <QtCore/QCoreApplication>

#include "WebserviceDispatcher.h"
#include "IResource.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

WebserviceDispatcher* myDispatcher = new WebserviceDispatcher(QHostAddress("localhost"), 8080);
IResource* service1 = new IResource("service1", myDispatcher, 0);
IResource* service2 = new IResource("service2", myDispatcher, 0);
IResource* subService1 = new IResource("subService1", myDispatcher, service1);
IResource* subService2 = new IResource("subService2", myDispatcher, service2);

service1->addService("sub1", subService1);
myDispatcher->addService("service1", service1);

service2->addService("sub2", subService2);
myDispatcher->addService("service2", service2);

myDispatcher->start();

return a.exec();
}


this example is working and lets you navigate to the uris:

http://localhost:8080/
http://localhost:8080/service1/
http://localhost:8080/service2/
http://localhost:8080/service1/sub1/
http://localhost:8080/service2/sub2/


The service shall provide and accept json objects.
I am not an expert on the topic so any opinion is appreciated (more precisely: is that the intended use of the qxt-classes? Has anyone done that better already and is willing to share ideas?)

Thanks

yuzhiyuxia
24th September 2013, 09:05
hi,
why do I not run it well in my computer.I create the project with vs2008 + QT.Now I can't response to the browser.

rspock
29th October 2013, 08:48
Ok, well I tried and everything seems ok.

I've a question: how can I use last string in the path as parameters?
For example I'd like to do: http://localhost:8080/service1/sub1/1 where 1 is a parameter to pass to the sub1 not another service to register.

Thanks