Tuesday, August 19, 2014

JAX-WS JavaSE Service endpoint

JavaSE 6+ has support for publishing Webservice. The java version understands the @Webservice annotation.

Let's make a plain Java class:

@WebService
public class TemperatureConvertor {
    
    public double celsiusToFarenheit(double temp){
        return ((temp*1.8) + 32);
    }    
    public double farenheitToCelsius(double temp){
        return ((temp-32)/1.8);
    }

Generate the artifacts using wsgen tool. The tool will be located at your <JDK_HOME>/bin directory.

wsgen -keep -s <Directory where source to be generataed>
      -cp <Directory where Class files to be kept>
       <TemperatureConvertor class with full package>

Now write a main method:

TemperatureConvertor tc= new TemperatureConvertor();
Endpoint endpoint =     Endpoint.publish("http://localhost:8080/tempConv", tc);
System.out.println("Reached here");
//endpoint.stop(); //Uncommenting this will stop the service

Hit the wsdl at [http://example.com|http://localhost:8080/tempConv?wsdl]

Also you can hit the service at http://localhost:8080/tempConv using ((soapUI)) tool.

The service is hosted using an embedded HTTP server in JavaSE 6. Please refrain from using this server in production environment.

More on Web services

No comments:

Post a Comment