Consume values from a webservice

3

I do not have any knowledge of the use of webservice , and I need to use one now to consume data from a database (Usually I would fetch the data direct from the database, but in this case the admin did not authorize, the only way was to make a webservice available.)

So I have a page in PHP with several function names:

Example:

Lista_produtos

Question: How do I create a client in JAVA that consumes these values? Can you provide some sample code?

edit

    
asked by anonymous 22.07.2014 / 11:28

4 answers

1

First you should read the wsdl created by webservice and create an interface in your project that has access to the methods you need. For this you need (at least in this case) apache cxf. With this just use the command

[caminho]\apache-cxf-2.2.7\bin>wsdl2java -p [pacote que será criado] -d "[caminho onde será criado o pacote]" [seuWebservice?wsdl] 

If you have xml on your machine you can replace the url with the xml path

After that the method call is the same as any other call in an interface, following an example (here the webservice has a method called seePerIdCompany that will be consumed by the method returning a company)

  public Empresa consultarEmpresaPorId(Long id) {
    ConsultarEmpresaPorIdRequest request = new ConsultarEmpresaPorIdRequest();
    request.setIdEmpresa(id.intValue());
    ConsultarEmpresaPorIdResponse response = WEBSERVICE.consultarEmpresaPorId(request);
    if (response.getResultCode() == 100) {
        return response.getEmpresa();
    }
    
22.07.2014 / 15:31
1

First you need to choose what type of Webservice you are going to create and how you are going to create it.

There is this guide for creating Restful webservices and using JAX-WS: link

In addition, you can choose to follow this very good guide from the GUJ website: link

Or, if you want something quick, you can make a Stateless Session Bean available as a webservice by simply annotating it with the @WebService tag:

@Stateless  
@WebService  
public class MyWebService {  
   public double sum(double a, double b) {  
       return a + b;  
   }  
}  

More details here: link

    
22.07.2014 / 15:01
0

It seems simple enough. I found the code below on this link here .

URL url = new URL("http://argentumws.caelum.com.br/negociacoes");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream content = connection.getInputStream();
List<Negociacao> negociacoes = new LeitorXML().carrega(content);
    
22.07.2014 / 14:51
0

You can use the wsimport command that comes in the JDK itself.

Open cmd or terminal and enter wsimport -d diretorio onde vai colocar os arquivos gerados + endereço do wsdl -Xnocompile .

Here's a more explanatory tutorial on wsimport: link

Here's a tutorial on what to do after wsimport: link

Hug.

    
24.07.2014 / 18:24