Delphi:基于mORMot的REST API有什么问题? [英] Delphi: What's wrong with REST API based on mORMot?

查看:140
本文介绍了Delphi:基于mORMot的REST API有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试基于mORMot框架(Delphi)构建REST Web服务器,但是从远程数据库获取数据存在问题.

I try to build REST Web Server based on mORMot framework (Delphi) and have issue with getting data from remote DB.

Server.dpr

const
  CONN_STR =
    'DRIVER=SQL Server Native Client 10.0;UID=user;PWD=pass;server=server;'
    + 'Trusted_Connection=No;MARS_Connection=yes';
var
  Model: TSQLModel;
  RESTServer: TSQLRestServerDB;
  Application: TwoStreamgateApplication;
  HTTPServer: TSQLHttpServer;
  ODBCConnProp: TSQLDBConnectionPropertiesThreadSafe;
begin
  SQLite3Log.Family.Level := LOG_VERBOSE;
  SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;
  ODBCConnProp := TODBCConnectionProperties.Create('', CONN_STR, 'wo', 'wo');
  try
    ODBCConnProp.ThreadingMode := tmMainConnection;
    Model := CreateModel;
    VirtualTableExternalRegisterAll(Model, ODBCConnProp);
    try
      RESTServer := TSQLRestServerDB.Create(Model, ':memory:');
      try
        RESTServer.DB.Synchronous := smNormal;
        RESTServer.DB.LockingMode := lmExclusive;
        RESTServer.CreateMissingTables;
        Application := TwoStreamgateApplication.Create;
        try
          Application.Start(RESTServer);
          HTTPServer := TSQLHttpServer.Create('8092', [RESTServer]
{$IFNDEF ONLYUSEHTTPSOCKET}, '+', useHttpApiRegisteringURI{$ENDIF});
          try
            HTTPServer.RootRedirectToURI('api/default');
            RESTServer.RootRedirectGet := 'api/default';
            writeln('"MVC WOStreamgate Server" launched on port 8092 using ',
              HTTPServer.HttpServer.ClassName);
            writeln(#10'You can check http://localhost:8092/api/info for information');
            writeln('or point to http://localhost:8092 to access the web app.');
            writeln(#10'Press [Enter] to close the server.'#10);
            readln;
            writeln('HTTP server shutdown...');
          finally
            HTTPServer.Free;
          end;
        finally
          Application.Free;
        end;
      finally
        RESTServer.Free;
      end;
    finally
      Model.Free;
    end;
  finally
    ODBCConnProp.Free;
  end;

模型在下面的 ServerModel.pas 单元

type
  TSQLBaseData = class(TSQLRecord)
  private
    FName: RawUTF8;
    FDetailURL: RawUTF8;
    FLogoURL: RawUTF8;
    FDescription: RawUTF8;
  published
    property Name: RawUTF8 read FName write FName;
    property Description: RawUTF8 read FDescription write FDescription;
    property LogoURL: RawUTF8 read FLogoURL write FLogoURL;
    property DetailURL: RawUTF8 read FDetailURL write FDetailURL;
  end;

  TSQLStation = class(TSQLBaseData)
  end;

function CreateModel: TSQLModel;

implementation

function CreateModel: TSQLModel;
begin
  result := TSQLModel.Create(
    [TSQLStation],
    'api');
end;

ServerViewModel.pas 单元

TServerApplication = class(TMVCApplication, IServerApplication)
  public
    procedure Start(aServer: TSQLRestServer); reintroduce;
    procedure StationView(ID: TID; out Station: TSQLStation);
  end;

implementation 

procedure TServerApplication.Start(aServer: TSQLRestServer);
begin
  inherited Start(aServer,TypeInfo(IServerApplication));
  fMainRunner := TMVCRunOnRestServer.Create(Self).
    SetCache('Default',cacheRootIfNoSession,15);
  aServer.Cache.SetCache(TSQLStation);
end;

procedure TServerApplication.StationView(ID: TID; out Station: TSQLStation);
begin
  RestModel.Retrieve(ID, Station);
end;

现在,一旦我在浏览器中键入 http://localhost:8092/api/station/10 ,我就会得到响应

and now once I type http://localhost:8092/api/station/10 in browser, I get response

{"errorCode":404,"errorText":未找到"}

{ "errorCode":404, "errorText":"Not Found" }

如果我在浏览器中输入 http://localhost:8092/api/stationview/10 ,则会显示页面,但没有有关 station = 10 或任何其他信息其他.

in case I type http://localhost:8092/api/stationview/10 in browser I get rendered page, but without info about station=10 or any other.

我在哪里弄错了?我只想简单地获取对我的请求的JSON响应

Where did I make a mistake? I want simply get JSON-response to my request

更新:

可以提供帮助的其他信息.一旦我尝试查询

Another info that can help. Once I try the query

procedure TestQuery();
var
  SQLDBConnection: TODBCConnection;
  Q: TQuery;
begin
  SQLDBConnection := TODBCConnection.Create(ODBCConnProp);
  Q := TQuery.Create(SQLDBConnection);
  try
    Q.SQL.Clear; // optional
    Q.SQL.Add('select * from dbo.Station');
    Q.Open;
...

我遇到一个错误:

Project Server.exe引发了带有消息'TODBCStatement-TODBCLib的异常类EODBCException错误:[HY106] [Microsoft] [SQL Server本机客户端10.0]访存类型超出范围(0)'.

Project Server.exe raised exception class EODBCException with message 'TODBCStatement - TODBCLib error: [HY106] [Microsoft][SQL Server Native Client 10.0]Fetch type out of range (0) '.

推荐答案

解决方案非常简单-需要添加:

Solution is very simple - need to add:

Database = myDataBase;

CONN_STR ='DRIVER = SQL Server本机客户端10.0; UID = user; PWD = pass; server = server;'+'Trusted_Connection =否; MARS_Connection =是';数据库= myDataBase;

CONN_STR = 'DRIVER=SQL Server Native Client 10.0;UID=user;PWD=pass;server=server;' + 'Trusted_Connection=No;MARS_Connection=yes';Database=myDataBase;

感谢Arnaud提供了出色的框架!

Thanks Arnaud for great framework!!!

这篇关于Delphi:基于mORMot的REST API有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆