I'd like to know what to do to implement a client for a webservice
in Eclipse as a Dynamic Web Project
. I can not generate a client, so I have to start from scratch.
I'd like to know what to do to implement a client for a webservice
in Eclipse as a Dynamic Web Project
. I can not generate a client, so I have to start from scratch.
Emanuella Gomes , I will answer your question, based on your comments (without specifying required technologies or rules).
As you already have an implanted webservice you will need the methods that already exist in it (if you do not have to create and deploy again).
Example of a dummy method in its Webservice
@GET
@Path("/NovoModulo")
@Produces("application/json")
@Consumes("application/json")
public String listarFaturas()
{
String gson = "";
try
{
gson = new Gson().toJson(sincronizador.exportarFaturasREST());
} catch (SQLException e)
{
e.printStackTrace();
gson = e.getMessage();
}
return gson;
}
This Gson I particularly enjoy using because we can generate a Json from objects and it's very easy to work.
I imagine you already know how to create and configure a Dynamic Web Project
so from that, you need to develop your new module according to specifications (if any) and implement access to your WebService
.
Example access to this method via Java in its DWP
:
private final String metodo = "NovoMetodo/";
public List<FaturaPendente> listarFaturas() throws Exception
{
ConexaoWebService conexaoWebService = ConexaoWebService.getInstance();
//Aqui você poderia criar uma validação para garantir que existe seus dados de oconexão
//como ip, porta e nome do webservice
//Aqui podemos chamar de "manha" você monta uma URL com os dados que foram verificados acima
URL_WS = conexaoWebService.getProtocolor() + conexaoWebService.getIp() + conexaoWebService.getPorta() + conexaoWebService.getWebservice() + conexaoWebService.getClasse();
//aqui é onde você monta o acesso ao seu método exemplo : 192.168.0.200:8080/WebService/NovoMetodo
String[] resposta = new WebServiceCliente().get(URL_WS + metodo);
List<Fatura> faturas = new ArrayList<Fatura>();
//se a resposta for OK retornará 200 e você terá acesso aos dados do webservice
if (resposta[0].equals("200"))
{
//utilizando o Gson
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(resposta[1]).getAsJsonArray();
for (int i = 0; i < array.size(); i++)
{
FaturaImportado faturaImportada = gson.fromJson(array.get(i), FaturaImportado.class);
Fatura fatura = new Fatura(faturaImportada.getCodigoCliente(), faturaImportada.getNomeCliente(), faturaImportada.getFormaPagamento());
faturas.add(fatura);
}
}
return faturas;
}
An idea for you is to put in some button to trigger this check, or every time the user enters that module list what you need.
** Return invoices : Since your object is already filled at this time you can use it in the way that best meets your needs and use the result to display your information.
Remembering that this is a new module, your WebService needs to have the methods that will be consumed by your "New Module" application.