Simple SOAP Server in Java

Create a Web Application (MyWebServices) in NetBeans. Create a class with annotations

@WebService()
public class MyWebService {

	@WebMethod(operationName = "printName")
	public String printName(@WebParam(name = "userName") String userName) {

		return "hello " + userName;
	}

	@WebMethod
	public int[] getArr() {
		int[] i = { 1, 1, 2, 3, 4 };
		return i;
	}
}

Map the path in web.xml file

<servlet>
    <servlet-name>MyWebService</servlet-name>
    <servlet-class>PACKAGE_NAME.MyWebService</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyWebService</servlet-name>
    <url-pattern>/MyWebService</url-pattern>
</servlet-mapping>

Deploy the project and the web service is ready to be used.Automatically the web service endpoint becomes like
http://localhost:8080/MyWebServices/MyWebService?wsdl

To test the web service I created a simple PHP soap client.

$soap = new SoapClient("http://localhost:8080/MyWebServices/MyWebService?wsdl",array(
										'trace' => true,
										'exceptions' => true,));
$name = $soap->printName(array("userName" => "Dave"));
$arr = $soap->getArr();
Advertisements