In this tutorial we are going to see how to create web service end point and client in RPC style SOAP binding . Follow the steps given below to create RPC style SOAP web service
1. Open Eclipse, Select File –> New --> Java Project
2. Create Java Project called WebService
3. Create package com.javatutorialscorner.jaxws.helloworld under WebService
4. Create Interface HelloWorld under package com.javatutorialscorner.jaxws.helloworld
HelloWorld.java
5. Create End Point Implementation class HelloWorldImpl under package com.javatutorialscorner.jaxws.helloworld
HelloWorldImpl.java
6.Create End point publisher class HelloWorldPublisher under package com.javatutorialscorner.jaxws.helloworld
HelloWorldPublisher .java
7.Now run the end point publisher class . Now your web service is published in the following URL
http://localhost:9080/helloworld
if you run the URL in web browser you can see the following screen

8.You can test the deployed web service by accessing the generated WSDL file via http://localhost:9080/helloworld?wsdl this URL
Generated WDSL file
9.You can access the web service in following way
1. Create New Java Project called WebServiceClient
2.Create package com.javatutorialscorner.jaxws.client under WebServiceClient
3. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client
HelloWorldClient.java
4. Run the client see the following output in console
Access the web service using wsimport tool
1. Create New Java Project called WebServiceClient
2. Generate client stubs by running the following commads
or
3. You can see the one classes and one interfaces generated in WebServiceClient project’s com.javatutorialscorner.jaxws.helloworld package.i.e
HelloWorldImplService.java
4. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client to access the generated service
HelloWorldClient.java
5. Run the client see the following output in console
1. Open Eclipse, Select File –> New --> Java Project
2. Create Java Project called WebService
3. Create package com.javatutorialscorner.jaxws.helloworld under WebService
4. Create Interface HelloWorld under package com.javatutorialscorner.jaxws.helloworld
HelloWorld.java
01.
package
com.javatutorialscorner.jaxws.helloworld;
02.
03.
import
javax.jws.WebMethod;
04.
import
javax.jws.WebService;
05.
import
javax.jws.soap.SOAPBinding;
06.
import
javax.jws.soap.SOAPBinding.Style;
07.
08.
@WebService
09.
@SOAPBinding
(style = Style.RPC)
10.
public
interface
HelloWorld {
11.
@WebMethod
12.
public
String sayHello(String message);
13.
}
5. Create End Point Implementation class HelloWorldImpl under package com.javatutorialscorner.jaxws.helloworld
HelloWorldImpl.java
01.
package
com.javatutorialscorner.jaxws.helloworld;
02.
03.
import
javax.jws.WebService;
04.
05.
@WebService
(endpointInterface =
"com.javatutorialscorner.jaxws.helloworld.HelloWorld"
)
06.
public
class
HelloWorldImpl
implements
HelloWorld {
07.
08.
@Override
09.
public
String sayHello(String message) {
10.
// TODO Auto-generated method stub
11.
return
"Java Tutorials Corner Says "
+ message;
12.
}
13.
14.
}
6.Create End point publisher class HelloWorldPublisher under package com.javatutorialscorner.jaxws.helloworld
HelloWorldPublisher .java
01.
package
com.javatutorialscorner.jaxws.helloworld;
02.
03.
import
javax.xml.ws.Endpoint;
04.
05.
public
class
HelloWorldPublisher {
06.
07.
public
static
void
main(String[] args) {
08.
// TODO Auto-generated method stub
09.
Endpoint.publish(
"http://localhost:9080/helloworld"
,
10.
new
HelloWorldImpl());
11.
}
12.
13.
}
7.Now run the end point publisher class . Now your web service is published in the following URL
http://localhost:9080/helloworld
if you run the URL in web browser you can see the following screen

8.You can test the deployed web service by accessing the generated WSDL file via http://localhost:9080/helloworld?wsdl this URL
Generated WDSL file
01.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
02.
<!-- Published by JAX-WS RI at http:
//jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
03.
<!-- Generated by JAX-WS RI at http:
//jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
04.
<definitions xmlns:wsu=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp=
"http://www.w3.org/ns/ws-policy"
xmlns:wsp1_2=
"http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsam=
"http://www.w3.org/2007/05/addressing/metadata"
xmlns:soap=
"http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns=
"http://helloworld.jaxws.javatutorialscorner.com/"
xmlns:xsd=
"http://www.w3.org/2001/XMLSchema"
xmlns=
"http://schemas.xmlsoap.org/wsdl/"
targetNamespace=
"http://helloworld.jaxws.javatutorialscorner.com/"
name=
"HelloWorldImplService"
>
05.
<types/>
06.
<message name=
"sayHello"
>
07.
<part name=
"arg0"
type=
"xsd:string"
/>
08.
</message>
09.
<message name=
"sayHelloResponse"
>
10.
<part name=
"return"
type=
"xsd:string"
/>
11.
</message>
12.
<portType name=
"HelloWorld"
>
13.
<operation name=
"sayHello"
>
14.
<input wsam:Action=
"http://helloworld.jaxws.javatutorialscorner.com/HelloWorld/sayHelloRequest"
message=
"tns:sayHello"
/>
15.
<output wsam:Action=
"http://helloworld.jaxws.javatutorialscorner.com/HelloWorld/sayHelloResponse"
message=
"tns:sayHelloResponse"
/>
16.
</operation>
17.
</portType>
18.
<binding name=
"HelloWorldImplPortBinding"
type=
"tns:HelloWorld"
>
19.
<soap:binding transport=
"http://schemas.xmlsoap.org/soap/http"
style=
"rpc"
/>
20.
<operation name=
"sayHello"
>
21.
<soap:operation soapAction=
""
/>
22.
<input>
23.
<soap:body use=
"literal"
namespace=
"http://helloworld.jaxws.javatutorialscorner.com/"
/>
24.
</input>
25.
<output>
26.
<soap:body use=
"literal"
namespace=
"http://helloworld.jaxws.javatutorialscorner.com/"
/>
27.
</output>
28.
</operation>
29.
</binding>
30.
<service name=
"HelloWorldImplService"
>
31.
<port name=
"HelloWorldImplPort"
binding=
"tns:HelloWorldImplPortBinding"
>
32.
<soap:address location=
"http://localhost:9080/helloworld"
/>
33.
</port>
34.
</service>
35.
</definitions>
9.You can access the web service in following way
- Access the web service using Java Client
- Access the web service using wsimport tool
1. Create New Java Project called WebServiceClient
2.Create package com.javatutorialscorner.jaxws.client under WebServiceClient
3. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client
HelloWorldClient.java
01.
package
com.javatutorialscorner.jaxws.client;
02.
03.
import
java.net.MalformedURLException;
04.
import
java.net.URL;
05.
06.
import
javax.xml.namespace.QName;
07.
import
javax.xml.ws.Service;
08.
09.
import
com.javatutorialscorner.jaxws.helloworld.HelloWorld;
10.
11.
public
class
HelloWorldClient {
12.
13.
/**
14.
* @param args
15.
*/
16.
public
static
void
main(String[] args) {
17.
// TODO Auto-generated method stub
18.
try
{
19.
URL url =
new
URL(
"http://localhost:9080/helloworld?wsdl"
);
20.
QName qName =
new
QName(
22.
"HelloWorldImplService"
);
23.
Service service = Service.create(url, qName);
24.
HelloWorld helloWorld = service.getPort(HelloWorld.
class
);
25.
System.out.println(
"Web Service Message "
26.
+ helloWorld.sayHello(
"Hello"
));
27.
}
catch
(MalformedURLException e) {
28.
// TODO Auto-generated catch block
29.
e.printStackTrace();
30.
}
31.
32.
}
33.
34.
}
4. Run the client see the following output in console
HelloWorldClient.java
Web Service Message Java Tutorials Corner Says Hello
Access the web service using wsimport tool
1. Create New Java Project called WebServiceClient
2. Generate client stubs by running the following commads
1.
cd %project_home%/src wsimport -s . http:
//localhost:9080/helloworld?wsdl
or
1.
cd %project_home%/src wsimport -keep http:
//localhost:9080/helloworld?wsdl
3. You can see the one classes and one interfaces generated in WebServiceClient project’s com.javatutorialscorner.jaxws.helloworld package.i.e
- interface HelloWorld.java
- class HelloWorldImplService.java
01.
package
com.javatutorialscorner.jaxws.helloworld;
02.
03.
import
javax.jws.WebMethod;
04.
import
javax.jws.WebParam;
05.
import
javax.jws.WebResult;
06.
import
javax.jws.WebService;
07.
import
javax.jws.soap.SOAPBinding;
08.
09.
10.
/**
11.
* This class was generated by the JAXWS SI.
12.
* JAX-WS RI 2.0_02-b08-fcs
13.
* Generated source version: 2.0
14.
*
15.
*/
16.
@WebService
(name =
"HelloWorld"
, targetNamespace =
"http://helloworld.jaxws.javatutorialscorner.com/"
)
17.
@SOAPBinding
(style = SOAPBinding.Style.RPC)
18.
public
interface
HelloWorld {
19.
20.
21.
/**
22.
*
23.
* @param arg0
24.
* @return
25.
* returns java.lang.String
26.
*/
27.
@WebMethod
28.
@WebResult
(partName =
"return"
)
29.
public
String sayHello(
30.
@WebParam
(name =
"arg0"
, partName =
"arg0"
)
31.
String arg0);
32.
33.
}
HelloWorldImplService.java
01.
package
com.javatutorialscorner.jaxws.helloworld;
02.
03.
import
java.net.MalformedURLException;
04.
import
java.net.URL;
05.
import
javax.xml.namespace.QName;
06.
import
javax.xml.ws.Service;
07.
import
javax.xml.ws.WebEndpoint;
08.
import
javax.xml.ws.WebServiceClient;
09.
10.
11.
/**
12.
* This class was generated by the JAXWS SI.
13.
* JAX-WS RI 2.0_02-b08-fcs
14.
* Generated source version: 2.0
15.
*
16.
*/
17.
@WebServiceClient
(name =
"HelloWorldImplService"
, targetNamespace =
"http://helloworld.jaxws.javatutorialscorner.com/"
, wsdlLocation =
"http://localhost:9080/helloworld?wsdl"
)
18.
public
class
HelloWorldImplService
19.
extends
Service
20.
{
21.
22.
private
final
static
URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
23.
24.
static
{
25.
URL url =
null
;
26.
try
{
27.
url =
new
URL(
"http://localhost:9080/helloworld?wsdl"
);
28.
}
catch
(MalformedURLException e) {
29.
e.printStackTrace();
30.
}
31.
HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
32.
}
33.
34.
public
HelloWorldImplService(URL wsdlLocation, QName serviceName) {
35.
super
(wsdlLocation, serviceName);
36.
}
37.
38.
public
HelloWorldImplService() {
39.
super
(HELLOWORLDIMPLSERVICE_WSDL_LOCATION,
new
QName(
"http://helloworld.jaxws.javatutorialscorner.com/"
,
"HelloWorldImplService"
));
40.
}
41.
42.
/**
43.
*
44.
* @return
45.
* returns HelloWorld
46.
*/
47.
@WebEndpoint
(name =
"HelloWorldImplPort"
)
48.
public
HelloWorld getHelloWorldImplPort() {
49.
return
(HelloWorld)
super
.getPort(
new
QName(
"http://helloworld.jaxws.javatutorialscorner.com/"
,
"HelloWorldImplPort"
), HelloWorld.
class
);
50.
}
51.
52.
}
4. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client to access the generated service
HelloWorldClient.java
01.
package
com.javatutorialscorner.jaxws.client;
02.
03.
import
com.javatutorialscorner.jaxws.helloworld.HelloWorld;
04.
import
com.javatutorialscorner.jaxws.helloworld.HelloWorldImplService;
05.
06.
public
class
HelloWorldClient {
07.
public
static
void
main(String[] args) {
08.
HelloWorldImplService service =
new
HelloWorldImplService();
09.
HelloWorld helloWorld = service.getHelloWorldImplPort();
10.
System.out.println(
"Web Service message : "
11.
+ helloWorld.sayHello(
"Hello"
));
12.
}
13.
}
5. Run the client see the following output in console
Web Service message : Java Tutorials Corner Says Hello
0 comments:
Post a Comment