本文介绍了如何使用Wt启动和运行Restful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Web上的教程的正在运行的Wt应用程序,我想知道是否有一种优雅的方式使用Wt添加一些Restful API功能.

I have a running Wt application based on the tutorials all over the web and I was wondering if there is an elegant way of using Wt to add a some Restful API functionality.

我有一些可以从当前应用程序中获取的资源,并且我不想实现任何补丁.

I have a few resources I can expose from my current application and I don't want to implement any patches.

如果有人对如何做到这一点有很好的主意,或者甚至对可以使开发变得轻而易举的JSON库的建议,我将非常感激.

If someone has a good idea of how to do that, or even a suggestion of some JSON library that can make the development a breeze, I'd be very thankful.

推荐答案

您应该将WResource子类化,并实现WResource::handleRequest方法以提供REST API功能.然后,您应该使用WServer::addResource

You should subclass WResource and implement the WResource::handleRequest method to provide REST API functionality. Then you should add your resource to your server using WServer::addResource

此外,您还应确保将资源添加到服务器上Wt应用程序的主要入口点之前:

Also you should ensure that you add your resource to the server before the main Wt appliaction entry point:

Wt::WServer server(argv[0]);
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
server.addResource(new MyResource, "/api"));
server.addEntryPoint(Wt::Application, createApplication);

这篇关于如何使用Wt启动和运行Restful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:55