another-programmer
another-programmer

Reputation: 861

Java - using SOAP with generated from WSDL classes

I have a WSDL schema link. With NetBeans I generated classes from this schema. But I can't understand, how to use it to send request to server? There is a XXXImplService extends Service class generated by NetBeans, should I use it? How?

As I think, I need just to create objects (which match WSDL methods and classes), set necessary properties and somehow transform this objects into a text of request, then send it with and get text response, which I can transform into classes. Is this true?

Upvotes: 0

Views: 2379

Answers (3)

void
void

Reputation: 7890

of course you have to use the WSDL, follow below steps for a complete client app for a Java web service (JAX-WS):

assuming you have a Web Service like this:

@WebService
public class Hello {
    private String message = new String("Hello, ");

    public void Hello() {}

    @WebMethod
    public String sayHello(String name) {
        return message + name + ".";
    }
}
  1. Uses the javax.xml.ws.WebServiceRef annotation to declare a reference to a web service. @WebServiceRef uses the wsdlLocation element to specify the URI of the deployed service’s WSDL file:

    @WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/hello?wsdl") static HelloService service;

  2. Retrieves a proxy to the service, also known as a port, by invoking getHelloPort on the service.

    Hello port = service.getHelloPort(); The port implements the SEI defined by the service.

  3. Invokes the port’s sayHello method, passing to the service a name.

    String response = port.sayHello(name);
    

EDIT: (request in comments) if the web service request for basic authentication and want to pass a username and password you can pass them like this (there are other ways also):

import java.net.Authenticator;
import java.net.PasswordAuthentication;

Authenticator authenticator = new Authenticator() 
{
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication("usr", "pass".toCharArray());
    }
};

Authenticator.setDefault(authenticator );

however if you want authentication in application level not on basic HTTP this link can be useful.

Upvotes: 2

Jagath Ariyarathne
Jagath Ariyarathne

Reputation: 95

This tutorial will help you to do it step by step. Since you have already created stub classes, skip the first part. Focus on "Web service invocation" section.

http://www.ibm.com/developerworks/webservices/library/ws-apacheaxis/index.html?ca=dat

Upvotes: 1

Aninda Bhattacharyya
Aninda Bhattacharyya

Reputation: 1251

You have to implement your code in this generated service impl and web method now. So when you will be calling the service end point and a specific method, through a web service client ( SOAP UI etc), these generated classes will take the call and route through service impl, to your implementation.

Upvotes: 1

Related Questions