Jax-ws Web Service Client


Before we start, system configurations are belows:

java = 1.7
Project type = Maven
We use below dependency:


<dependency>
 <groupId>com.sun.xml.ws</groupId>
 <artifactId>jaxws-rt</artifactId>
 <version>2.1.7</version>
 <scope>compile</scope>
</dependency>

Eclipse : Luna Service Release 2 (4.4.2)

Githu source code is : git source code

If you need a web service to create its client you can see first web service example . . After you create your local service, you can pass this section.

1. Create a maven project with quick-start archetype.



2. Add jaxws-rt dependency (it's above) to your pom.xml

3. Create a java class, it containst http authentications.

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import com.example.service.HelloWorldServiceImplService;
import com.example.service.IHelloWorld;

public class HelloClient {

    public static void main(String[] args) {
        HelloWorldServiceImplService serviceFirst = new HelloWorldServiceImplService();
        IHelloWorld port = serviceFirst.getHelloWorldServiceImplPort();
   
       //This is for http authentications
        Map<String, Object> req_ctx = ((BindingProvider) port).getRequestContext();

        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        //If you write different username or password, it throw invalid user exception.
        headers.put("Username", Collections.singletonList("Jack"));
        headers.put("Password", Collections.singletonList("1234"));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        //authentication code end

        String serviceResponse = port.getHelloWorld("Jack");
       
        System.out.println(serviceResponse);
    }
} 

4.  Run cmd, cmd's path is your project webservice source folders' path( wsHello ). This will create entity files of webservice.

      C:\YourWorkspace\YourProject\wsHello> wsimport -keep -verbose http://localhost:8080/ws/hello?wsdl

5. delete *.class files with below commands.
      C:\YourWorkspace\YourProject\wsHello>del /s *.class

6. If your web service is running, this prints "Welcome, Jack".