WebService

6 respostas
Mancini

Oi galera td bem?

Tou com um problema, tou tentado fazer uma webservice para depois consumir. que é que quero ? quero inserir 3 ou 4 Strings e de acordo com essas strings (nao sei se precisa de validação) ele retornar uma valor. Por exemplo string brasil retorna valor 1. Se digitar String America, retorna valor 2. Alguem me ajuda?

Obrigado.

Mancini.

6 Respostas

romarcio

Mancini, a umas 2 semanas precisei aprender alguma coisa sobre webservice.
Que tipo de ajuda você realmente precisa?

Sabe iniciar o projeto do webservice?
Sabe criar a aplicação cliente?

Se for apenas o que você citou parece ser bem simples.

Mancini

Oi romarcio,

isso eu sei, eu preciso mesmo é de fazer o código pra isso. Depois eu sei aceder.

Consegue me ajudar?

Obrigado.

Mancini

romarcio

Bom, se você sabe inicar os projetos, vou dar dica nos métodos então:

No Web Service, você terá algo assim:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService()
public class StringInIntOut {

    /**
     * Operação de serviço web
     */
    @WebMethod(operationName = "stringInIntOut")
    public Integer stringInIntOut(@WebParam(name = "palavra")  String palavra) {

        Integer result = null;

        if (palavra.equalsIgnoreCase("Brasil")) {
          result = 1;
        } else if (palavra.equalsIgnoreCase("Argentina")) {
            result = 2;
        } else if (palavra.equalsIgnoreCase("Chile")) {
            result = 3;
        } else {
            result = 0;
        }
        return result;
    }

}

No cliente você teria algo assim:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        try { // Call Web Service Operation

            service4.StringInIntOutService service = new service4.StringInIntOutService();
            service4.StringInIntOut port = service.getStringInIntOutPort();

            String valor = jTextField1.getText();

            Integer result = port.stringInIntOut(valor);
            
            if (result != 0) {
              jTextArea1.setText("O País " + valor + " retorna: " + result.toString());
            } else {
              jTextArea1.setText("País não existe");  
            }
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

    }

Qualquer dúvida, só perguntar.

Mancini

Romarcio, Obrigado.

Vou testar já te digo alguma coisa.

Mancini.

Mancini

Romarcio,

Funcionou mt bem.

Excepto este método:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try { // Call Web Service Operation

service4.StringInIntOutService service = new service4.StringInIntOutService();

service4.StringInIntOut port = service.getStringInIntOutPort();

String valor = jTextField1.getText();

Integer result = port.stringInIntOut(valor);

if (result != 0) {

jTextArea1.setText("O País " + valor + " retorna: " + result.toString());

} else {

jTextArea1.setText(“País não existe”);

}

} catch (Exception ex) {

// TODO handle custom exceptions here

}

}

eu meti o metodo jButton1ActionPerformed fora da classe main, depois como chamo ele?

Obrigado.

Mancini.

romarcio

No caso eu fiz assim:

public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameStringInIntOut().setVisible(true);
            }
        });
    }

O método está dentro da classe JFrameStringInIntOut();

Não sei como você utilizou o método, mas é só chamar a classe que contém o método dentro do método main.

Criado 11 de dezembro de 2009
Ultima resposta 12 de dez. de 2009
Respostas 6
Participantes 2