Archive

Archive for March 1, 2014

Java RPC style Webservice with JAX-WS

We have to create two projects one is WebService server project and WebService client project, here client will call the methods of Server.

1. Creating Server project:

Create an interface with @WebService annotation, and annotate the method with @WebMethod whitch you wanted to expose as web service.

package com.java2practice.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface{
@WebMethod
int sum(int firstNumber, int secondNumber);
}

2. Create an implementation to the above interface  like below

package com.java2practice.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.java2practice.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{
@Override
public int sum(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}

3. Now we have to publish this webservice by using EndPoint.

package com.java2practice.ws;
import javax.xml.ws.Endpoint;
import com.java2practice.ws.WebServiceImpl;

public class WebServicePublisher{

public static void main(String[] args) {

Endpoint.publish("http://localhost:7777/webservice/testws", new WebServiceImpl());
}
}

 

  1. When we run the above code, webservice will be published and can be accessed by using http://localhost:7777/webservice/testws URL.
  2. To see the generated WSDL file click http://localhost:7777/webservice/testws?wsdl. And the generated WSDL look like
<pre>-<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<definitions targetNamespace="http://ws.java2practice.com/" name="WebServiceImplService"><types/>
<message name="sum"><part name="arg0" type="xsd:int"/><part name="arg1" type="xsd:int"/></message>
<message name="sumResponse"><part name="return" type="xsd:int"/></message>
<portType name="WebServiceInterface"><operation name="sum" parameterOrder="arg0 arg1">
<input wsam:Action="http://ws.java2practice.com/WebServiceInterface/sumRequest" message="tns:sum"/>
<output wsam:Action="http://ws.java2practice.com/WebServiceInterface/sumResponse" message="tns:sumResponse"/>
</operation></portType><binding name="WebServiceImplPortBinding" type="tns:WebServiceInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="sum"><soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://ws.java2practice.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://ws.java2practice.com/"/>
</output>
</operation>
</binding>
<service name="WebServiceImplService">
<port name="WebServiceImplPort" binding="tns:WebServiceImplPortBinding">
<soap:address location="http://localhost:7777/webservice/testws"/>
</port>
</service>
</definitions>

 

Our webservice server project is fine, now we have to create client program, for this we have to use wsimport command, this will be available in java.

  1. Go to command prompt and give wsimport it will show all the options which are there in wsimport, if it doesn’t show any thing set the path variable to jdk/bin.
  2. wsimport -keep http://localhost:7777/webservice/testws?wsdl -d D:\java2practice\Desktop\client
  3. It will generate the files, and copy those files to your IDE and create a client program like below.

package com.java2practice.ws;

public class WebServiceClient {

public static void main(String[] args) {

WebServiceImplService service = new WebServiceImplService();

WebServiceInterface serviceInterface = service.getWebServiceImplPort();

int sum = serviceInterface.sum(10, 20);

System.out.println("answer : "+sum);//outputs answer as 30

}

}

Thanks for reading.

 

Advertisements
Categories: Java, WebService

How to send multiple rows of data to Spring Controller

March 1, 2014 1 comment

Common requirement would be sending multiple rows of data to Spring Controller.

We use @RequestParam String value to hold the single form field. But when we dealing with multiple rows @RequestParam String value won’t work.

If It is single row like below like below

S.NO First Name Last Name Age
1 AAA BBB 23

We can use @RequestParam annotation to capture the form data:

public String saveEmployee(@RequestParam Integer sNo, @RequestParam String firstName, @RequestParam String firstName, @RequestParam Integer age){
//here we can access the form data.
}

Suppose we have a form with multiple rows. Then We have to use @RequestParam String[] values,

Let’s see the example below:

S.NO First Name Last Name Age
1 AAA BBB 23
2 CCC DDD 24
3 EEE FFF 28

And here is the Controller Code:


public String saveEmployees(@RequestParam Integer[] sNo, @RequestParam[] String firstName, @RequestParam[] String lastName, @RequestParam[] Integer age){

 //first row data
 Integer sNo_0 = sNo[0];
 String firstName_0 = firstName[0];
 String lastName_0 = lastName[0];
 Integer age_0 = age[0];

//second row data

 Integer sNo_1 = sNo[1];
 String firstName_1 = firstName[1];
 String lastName_1 = lastName[1];
 Integer age_1 = age[1];

//third row data

 Integer sNo_2 = sNo[2];
 String firstName_2 = firstName[2];
 String lastName_2 = lastName[2];
 Integer age_2 = age[2];
 }

Thanks for reading.

Categories: SQL