Create web service in Bottom Up approach using command line


Create Service Interface MyJaxWSSEI.java

package in.malliktalksjava.ws;

import in.malliktalksjava.ws.pojo.*;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.WebParam.Mode;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

@WebService(name = “MyJaxWSHello”,
targetNamespace = “http://malliktalksjava.in”,
wsdlLocation = “http://malliktalksjava.in/MyJaxWS?wsdl”)
@SOAPBinding(style=Style.RPC, use=Use.LITERAL)
public interface MyJaxWSSEI {

@WebMethod(operationName=”getJXWsRes”)
@RequestWrapper(targetNamespace=”http://malliktalksjava.in/ws/types”,
className=”java.lang.String”)
@ResponseWrapper(targetNamespace=”http://malliktalksjava.in/ws/types”,
className=”in.malliktalksjava.ws.JXRes”)
@WebResult(targetNamespace=”http://malliktalksjava.in/ws/types”,
name=”JXWsRes”)
public JXRes getJXWsRes(
@WebParam(targetNamespace=”http://malliktalksjava.in/ws/types”,
name=”name”,
mode=Mode.IN)
String name
);

}

 

Create Implementation class for the above interface – MyJaxWSSEIImpl.java

package in.malliktalksjava.ws;

import in.malliktalksjava.ws.pojo.*;
import javax.jws.WebService;

@WebService(endpointInterface=”in.malliktalksjava.ws.MyJaxWSSEI”,
targetNamespace=”http://malliktalksjava.in”,
portName=”MyJaxWSSEIPort”,
serviceName=”MyJaxWSHelloService”)
public class MyJaxWSSEIImpl implements MyJaxWSSEI {

/**
* Default Constructor
*/
public MyJaxWSSEIImpl() {

}

/* (non-Javadoc)
* @see in.malliktalksjava.ws.MyJaxWSSEI#getJXWsRes(java.lang.String)
*/
@Override
public JXRes getJXWsRes(String name) {
JXRes jxRes = new JXRes();
jxRes.setMessage(“Hello”);
jxRes.setName(name);
return jxRes;
}

}

Create Required Model object JXRes.java

package in.malliktalksjava.ws.pojo;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class JXRes {
protected String message;
protected String name;
/**
*
*/
public JXRes() {
super();
}
/**
* @return the id
*/
public String getMessage() {
return message;
}
/**
* @param id the id to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

}

Create Main Class JXResTest.java

package in.malliktalksjava.ws.main;

import javax.xml.ws.Endpoint;
import in.malliktalksjava.ws.*;

public class JXResTest {
public static void main(String[] args ){
Endpoint.publish(“http://localhost:8080/MyJaxWSService”, new MyJaxWSSEIImpl());
}
}

 

Compile Java Classes in command prompt:

compileclass

Run the class

runmainclass

Verify the service : http://localhost:8080/MyJaxWSService?wsdl

wsdl

 

Create a java webservice using STS


Below steps explains the how to create a web-service in java in bottom-up approach using the STS(Spring tool suite) IDE. In the bottom-up approach, first we will create a template class, using the template class we will generate the WSDL and deploy the service in servers. Follow the below steps to create a webservice.

Step 1: Create a new java project using the steps mentioned here

Step 2 : Create a template Java class as service

Create a Java class in src folder of the in.javatutorials package as below

Writeaclass

Provide the package name and class name into respective fields and click on finish button.

Namethetheclass

Implement the Java methods as give below:

write the class

Use the below source code to write the class:

package in.malliktalksjava;

/**
* Calculator class exposed as a webservice
* @author malliktalksjava
* @since Version 1.0
*
*/
public class Calculator {

/**
* adds the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer addition(int var1, int var2) {

Integer result = var1 + var2;
System.out.println(“addition result in service : ” + result);
return result;
}

/**
* multiply the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer multiplication(int var1, int var2) {

Integer result = var1 * var2;
System.out.println(“multiplication result in service : ” + result);
return result;
}

/**
* divide the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer division(int var1, int var2) {

Integer result = var1 / var2;
System.out.println(“division result in service : ” + result);
return result;
}

}

Step 3: make the java class as web service

Select the Java class as below

maketheclassas service

Choose Web Service from the  and click on Next button

createservice

Select the ‘Bottom up Java bean Web Service’  from the Web service type and service implementation class as mentioned in the below picture and click on Next button.

bottomup webservice

Select the service methods from the menu and click on Finish button. The Service implementation style used is document/literal as shown in below picture.

choose service methods

The folder structure of the web application looks like below:

webservice folder structure

Deploy the service into tomcat web server and access the WSDL file using the below url:

http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl

wsdlfile

Finally your web service is ready to use and WSDL url is the end point url for your web service.

 

Other Useful links:

 Click here to know more about webservices

Click here to know more about RESTfull web services.

Click here for Web services Question and Answers.

Click here to know how to write web service client suing java.