简体   繁体   中英

I'm stuck, how can I implement 2 different server methods in datasnap rest Delphi

Please, I need implement this

http://localhost:8080/datsnap/rest/clients/getinvoices

http://localhost:8080/datsnap/rest/suppliers/getorders

Where should I put the implementations for clients and suppliers?

Now I have the following code but my provider needs it to be implemented that way

My implementation:

http://localhost:8080/datsnap/rest/clients_getinvoices/1
http://localhost:8080/datsnap/rest/suppliers_getorders/100

Suppliers implementation

http://localhost:8080/datsnap/rest/clients/getinvoices/1
http://localhost:8080/datsnap/rest/suppliers/getorder/100**

The project RestServer consists of:

Initial form // where the server start stop
serverContainer
ServermethodsUnit1
WebModuleUnit1

Unit  ServerMethodsUni1

interface
uses system...... etc

type 

{$METHODINFO ON}
 TMyClass =class(TdataModule)
 FdConnection : TFDConeccion;
prvate
public
  function clients_getinvoices(id :string) : TjsonObject;
  function suppliers_getorders(id :string) : TjsonObject;
end;
{$METHODINFO OFF}
:
:
:

Thanks in advance.

Yes, as Marc suggested: add another DSServerClass component to the ServerContainerunit form. Connect that to a second servermethodsunit. The wizard will allow you to create one using File->New->Other->DataSnap->ServerModule.

Both DSServerClass components on the ServerContainerunit share the 1 DSServer via their Server properties. And rename your servermethods' TDSServerMethods instances. 1 as clients and 2 as suppliers . clients will have the getInvoices method, and suppliers will have the getorders method.

unit ServerMethodsUnit2;

interface

uses
  System.SysUtils, System.Classes, Datasnap.DSServer, 
  Datasnap.DSAuth, Datasnap.DSProviderDataModuleAdapter;

type
  TSuppliers = class(TDSServerModule)
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
  end;

implementation

uses
  System.StrUtils;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ Suppliers }

function TSuppliers.EchoString(Value: string): string;
begin
  Result := Value + ' ' + Value;
end;

function TSuppliers.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value);
end;

end.

And implement the onGetClass methods of the each of the 2 DSServerClass components, for example:

procedure TServerContainer1.DSServerClass2GetClass(
  DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
begin
  PersistentClass := ServerMethodsUnit2.TSuppliers;
end;

I Found the Solution,

Create a Suppliers class after

Unit  ServerMethodsUni1

interface
uses system...... etc

type 

{$METHODINFO ON}
 TMyClass =class(TdataModule)
 FdConnection : TFDConeccion;
prvate
public
  function clients_getinvoices(id :string) : TjsonObject;
  function suppliers_getorders(id :string) : TjsonObject;
end;
{$METHODINFO OFF}

Suppliers =class(TMyClass)
end;

Then in the ServerContainerUnit

procedure TServerContainer2.DSServerClass1GetClass(
  DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
begin
  PersistentClass := ServerMethodsUnit1.Suppliers;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM