Jax-ws Web Service Example



All source code's git address : source code

Before we start, system configurations are these:
java = 1.7
Project-Type = Maven

We'll use below dependency:

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


1. First of all, create a maven project with maven-archetype-quickstart . Enter groupId : com.exmaple
Artifact Id: HelloWebServiceExample.

2. Add jaxws-rt dependency to pom.xml
3.  Create an inteface as below:

package com.example.service;

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

@WebService
public interface IHelloWorld {

    @WebMethod
    String getHelloWorld(String param);

}



4. Create implementation of interface . At this point we add a http authentication . We get http request header. If header has suitable username and password, comsumer get service response. Otherwise gets "invalid user" message.

package com.example.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

@WebService(endpointInterface="com.example.service.IHelloWorld")
public class HelloWorldServiceImpl implements IHelloWorld {

    @Resource
    WebServiceContext webServiceContext;
   
    @Override
    public String getHelloWorld(String param) {
       
        return authenticateHeader(param);
    }
   
    private String authenticateHeader(String param){
        MessageContext messageContext = webServiceContext.getMessageContext();
        Map headers = (Map) messageContext.get(MessageContext.HTTP_REQUEST_HEADERS);
        List userNameList = (List) headers.get("username");
        List passwordList = (List) headers.get("password");
       
        if(userNameList != null && passwordList != null){
            boolean isValid = isExistingUser((String) userNameList.get(0), (String) passwordList.get(0));
            if(isValid){
                return "Welcome, " + param;
            }
        }
       
        throw new RuntimeException("Invalid User !!!!!!!!!!!");
    }

    private boolean isExistingUser(String userName, String password) {
       
        if(userName.equals("Jack") && password.equals("1234")){
            return true;
        }
       
        return false;
    }

}



5. Add a service publisher class.

package com.example.service;

import javax.xml.ws.Endpoint;

public class HelloPublisher {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/ws/hello", new HelloWorldServiceImpl());
    }
}



6. when you run HelloPublisher you can see result on browser. add "http://localhost:8080/ws/hello" on browser's url. you see this view.


7. Create service entity class.  Run below command in cmd. Cmd's path must be ServiceImpl class' path.

C:\yourWorkSpace\YourProject\com\example\service>  javac com/example/service/HelloWorldServiceImpl.java

Explanation of above command:
-verbose : print outputs of commands in cmd
-cp : path of creation files
-keep : keeps source files
. : point is  path of create files.

C:\yourWorkSpace\YourProject\com\example\service> wsgen -verbose -keep -cp . com.example.service.HelloWorldServiceImpl

8. Move created entity files to your project source folder (its name can be wsHello) .