Change a record with a REST Webservice

Asked

Viewed 128 times

0

I am trying to change the registration of my bank through a web service in java Rest, the tests in the webservice worked, but in the client I am having problem to work.

Note: Using Delphi XE8(Client) and Netbeans(WS))

WS:

@PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Path ("Banco/put/{codigo}")
    public String putBanco(@PathParam("codigo") int codigo, String nome) {

        BancoCTR ctr = new BancoCTR();
        ctr.setBcoNome(nome);

        BancoDAO dao = new BancoDAO();
        String resposta = dao.editBanco(codigo, ctr);

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

When I do the direct test, it works:

inserir a descrição da imagem aqui

but I have a doubt, public String putBanco(@PathParam("codigo") int código, String nome) Pathparam gives me access to this variable "code", but how I will have access to the variable "name" in the client?

Client:

procedure TForm1.btn2Click(Sender: TObject);
var
  LJson: TJSONValue;
begin
  rstrspns1.RootElement := 'object';

  with rstrqst1 do
  begin

    Resource := '/Banco/put/{codigo}';
    Method := rmPUT;

    Params.AddUrlSegment('codigo', edt1.Text);
    Params.AddItem('nome', edt2.Text, TRESTRequestParameterKind.pkGETorPOST);
    Execute;

    LJson := Response.JSONValue As TJSONObject;

    MessageDlg(LJson.ToString, mtInformation, [mbOK], 0);
  end;
end;

thought I’d use Params.AddItem('nome', edt2.Text, TRESTRequestParameterKind.pkGETorPOST); to access the name variable, but when I run I get the errors:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

when having debug:

the error occurs on the line Execute;

inserir a descrição da imagem aqui

1 answer

1


Well, as you didn’t specify, I’ll start from the premise that you are using JAX-RS in your Controller. When vc does not specify the type of parameter, JAX understands that you want to use it as the body of the request.

In this case, you must specify in your client the content-type of the request, and pass the name of the bank in the body. In this case, both "text/Plain" and "application/json" should work, since every String is a valid Json.

Another possibility would be to use @Queryparam, and pass its value as a Parameter query. It would look more or less like this the url:

http://localhost:8080/{seuContexto}/Banco/put/{codigo}?nome={nomeDoSeuBanco}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.