Consume values from a webservice

Asked

Viewed 3,679 times

3

I have no knowledge in the use of webservice, and I need to use one now to consume data from a database(Normally I would fetch the data directly from the database, but in this case the admin did not authorize, said the only way was to provide a webservice).

So I have a page on PHP with various function names:

Example:

Lista_produtos

Doubt: How do I create a customer in JAVA that consumes these values? They can provide some example code?

Edit

inserir a descrição da imagem aqui

4 answers

1

First you need to choose which type of Webservice you will create and how you will create.

There is this guide to create Restful webservices and with the use of JAX-WS: http://www.k19.com.br/artigos/criando-um-webservice-restful-em-java/

Also, you can choose to follow this very good guide from the GUJ website: http://www.guj.com.br/articles/132

Or, if you want something fast, you can make a Stateless Session Bean available as a webservice by simply announcing it with the @Webservice tag:

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

More details here: http://docs.oracle.com/javaee/5/tutorial/doc/bnbor.html

  • I do not need to create the webservice, only the client that in this case will be my desktop program, however I will check better the links may be I have what I need

  • If your program needs to consume information from the webservice, you will at least have to integrate it into your application...rs#Xa;If my answers are useful, mark as correct! (Thank you!

  • Yes has logic, only now I will test, as to mark as right do not worry...

1


First you should read the wsdl created by the 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. Simply 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 interface call, follow an example (here the webservice has a method called query PorId 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();
    }
  • Your answer seems to me the right one only I have a problem when I try to do the operation to read the wsdl, gives a mistake like "Use SOAP Enconding is not supported" already happened to you this?

  • The "Use SOAP Enconding is not supported" error solved itself with the "JAX-RPC plugin installation"

0

It looks pretty simple. I found the code below in 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);
  • Thank you for trying to help, but that code doesn’t work, or at least I can’t get it to work for my case. Actually, I’ve seen that code before, but the last part I don’t know what I’m supposed to put List<Negociacao> negociacoes = new LeitorXML().carrega(content); i do not have class negotiations, nor the xml reader(should be some lib)

  • here shows which library to use xml and how to use it: http://www.caelum.com.br/apostila-java-testes-jsf-web-services-design-patterns/workingcom-xml/#4-5-exercicios-reading-o-xml

0

Browser other questions tagged

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