Consume WebService in delphi

0

I created a REST webservice using JAVA, however I need to make a client in Delphi consume this webservice, I tried to make several video classes I found through google, but none was useful.

I created a simple webservice to test this communication with delphi and WS REST, the WS idea is to do an insert, update, delete and select banks, the WS works right with the tests I did, but to consume in delphi not sure, first I'm just testing the GET.

WS:

Obs1: I used netbeans to create WS.

Note: The GET method in question is @Path ("Bank / get / {name}"), the other GET methods were just tests to see how WS works.

Obs3: I do not think I need to put the classes with insert, select and etc methods, if so, I edit the post.

package ws;

import CTR.BancoCTR;
import com.google.gson.Gson;
import dao.BancoDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

/**
 * REST Web Service
 *
 * @author 
 */
@Path("BancoWs")
public class BancoWs {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of BancoWs
     */
    public BancoWs() {
    }

    /**
     * Retrieves representation of an instance of ws.BancoWs
     * @return an instance of java.lang.String
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getJson() {
        return "meu primeiro webservice restfull";
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/get/{nome}")
    public String getBanco(@PathParam("nome") String nome){
        List<BancoCTR> lista = new ArrayList<BancoCTR>();
        BancoDAO banco = new BancoDAO();

        if (nome.equals("null")){
            lista = banco.listBanco(0, nome);
        }else{
            lista = banco.listBanco(1, nome);
        }

        Gson gson = new Gson();
        return gson.toJson(lista);
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/list")
    public String listBancos(){

        List<BancoCTR> lista = new ArrayList<BancoCTR>();

        BancoCTR banco = new BancoCTR();
        banco.setBcoCodigo(1);
        banco.setBcoNome("Santander");

        lista.add(banco);

        banco.setBcoCodigo(2);
        banco.setBcoNome("Banco do Brasil");

        lista.add(banco);

        Gson gson = new Gson();
        return gson.toJson(lista);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/listBd")
    public String listBdBancos(){

        List<BancoCTR> lista = new ArrayList<BancoCTR>();

        BancoDAO dao = new BancoDAO();
        lista = dao.listBanco(0, "");

        Gson gson = new Gson();
        return gson.toJson(lista);
    }

    /**
     * PUT method for updating or creating an instance of BancoWs
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void putJson(String content) {
    }
}

Client:

the screen is very simple:

I tried to do a dbgrid, but an example I found used memo, so I tried to use it to see if it worked and then I would try dbgrid, but when I click the search button, it clears the memo more I do not have any information and it goes blank, I changed the code several times, but without success the current code is in the middle, because I was doing an example and noticed in the middle that it was not useful (I think) kkkkk.

Note: I'm using Delphi XE8.

procedure TForm1.btnPesquisarClick(Sender: TObject);
var
 filtro: String;
 mymarshal: TJSONUnMarshal;
begin
  rstrqst1.Resource := '/Banco/get/{nome}';
  rstrqst1.Method := rmGet;

  if edt3.Text <> '' then
    filtro := edt3.Text
  else
    filtro := 'null';

  rstrqst1.Params.AddUrlSegment('nome', filtro);
  rstrqst1.Execute;
  mymarshal := TJSONUnMarshal.Create;




  mmo1.Clear;
  mmo1.Lines.Add(rstrspns1.);
end;

Unfortunately so far I have not found any other examples that I can follow, I found some components like idHttp or TFDStanStorageJSONLink, but I was in doubt if they served, so I decided to ask here.

    
asked by anonymous 01.02.2018 / 17:39

1 answer

1

I use IdFtp extensively in my applications, it has been up to date on everything I needed in Rest. Here is a lean code example I use:

var
  http           : TIdHTTP;  
  AStrResponse   : TStringStream;
  oSSL           : TIdSSLIOHandlerSocketOpenSSL;  
  FURL           : String;
begin
  Result:= FALSE;

  http:= TIdHTTP.Create(nil);
  oSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

    oSSL.SSLOptions.Method      := sslvSSLv23;
    oSSL.SSLOptions.Mode        := sslmUnassigned;
    oSSL.SSLOptions.VerifyMode  := [];
    oSSL.SSLOptions.VerifyDepth := 0;
    oSSL.host                   := '';

    http.IOHandler       := oSSL;

    http.HandleRedirects := TRUE;

    http.request.ContentType := 'application/json';

    FURL := 'http://a_sua_url.com/alguma_coisa'

    AStrResponse := TStringStream.Create;

    http.Get(FURL, AStrResponse);
    Response.Text := AStrResponse.DataString;

    FreeAndNil(AStrResponse);

    http.Disconnect;

  FreeAndNil(oSSL);
  FreeAndNil(http);

end;
    
01.02.2018 / 20:40