Change a record with a REST WebService

0

I'm trying to change my bank's registry via a webservice rest in java, the tests in webservice worked, but on the client I'm having trouble working.

Note: I use 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 test, it is working:

ButIhaveaquestion,publicStringputBanco(@PathParam("codigo") int código, String nome) PathParam causes me to have access to this variable "code", but how will I have access to 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;

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

Whendebugging:

erroroccursonlineExecute;

    
asked by anonymous 07.02.2018 / 13:55

1 answer

1

Well, as you did not specify, I will start from the premise that you are using JAX-RS on your Controller. When you do not specify the type of parameter, JAX understands that you want to use it as the request body.

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 query parameter. It would look like this to the url:

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