Thursday, May 03, 2012

Java Web Service Simple Practicals..


Web Service Practical’s:
Open Eclipse :
Go File --> New --> Java Project :


Provide name and Click on Next..Then Finish..

    


In src folder create new package and classes as shown in Fig:

 

Code :
package com.demo.sayhello;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface SayHello {
@WebMethod
public String sayhello(String name);
}
------------------------------------------------------------------------------ 
package com.demo.sayhello.impl;

import javax.jws.WebService;

@WebService(endpointInterface = "com.demo.sayhello.SayHello")
public class SayHelloImpl implements com.demo.sayhello.SayHello {

@Override
public String sayhello(String name) {
System.out.println("Hello "+ name);
return "Hello"+name;
}

}
---------------------------------------------------------------------------------- 

package com.demo.app;

import javax.xml.ws.Endpoint;

import com.demo.sayhello.impl.SayHelloImpl;

public class SayHelloApp {

/**
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//http://localhost:9090/WS/sayHello?wsdl
Endpoint.publish("http://localhost:9090/WS/sayHello",
new SayHelloImpl());
}

}

Now run SayHelloApp.java  (Keeping it running.)
Jul 17, 2012 1:06:59 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.demo.sayhello.jaxws.Sayhello
Jul 17, 2012 1:06:59 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.demo.sayhello.jaxws.SayhelloResponse

And open url http://localhost:9090/WS/sayHello?wsdl






This is called WSDl file.This are web services hosted on http://localhost:9090/WS/sayHello?wsdl
Now we need to make available these services to client so client can use without knowing actual implementation.

Create new project WS-Client-Test

 

Now go to D:\GreatEasternProject\eclipse_Liferay_IDE_ws\WS-Client-Test\src from command prompt

D:\GreatEasternProject\eclipse_Liferay_IDE_ws\WS-Client-Test\src>wsimport -keep

 

Now in Eclipse refresh folder src of WS-Client –Test.

 

So code to access web services is get generated.

wsimport command for JAX-WS applications

The wsimport command-line tool processes an existing Web Services Description Language (WSDL) file and generates the required artifacts for developing Java API for XML-Based Web Services (JAX-WS) Web service applications. see

Create new package com.demo.client and class Client.java
package com.demo.client;

import com.demo.sayhello.impl.SayHello;
import com.demo.sayhello.impl.SayHelloImplService;

public class Client {

/**
@param args
*/
public static void main(String[] args) {
SayHelloImplService  service = new SayHelloImplService();
SayHello sayhello = service.getSayHelloImplPort();
sayhello.sayhello("Darshan");
}

}

Run Clint.java and output: Hello Darshan
Now if u stops SayHelloApp.java, then http://localhost:9090/WS/sayHello?wsdl will not be available and Client.java will give following Exception.
Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:9090/WS/sayHello?wsdl. It failed with:
http://localhost:9090/WS/sayHello?wsdl.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.(Unknown Source)
at com.demo.sayhello.impl.SayHelloImplService.(SayHelloImplService.java:46)
at com.demo.client.Client.main(Client.java:12)
Caused by: java.io.FileNotFoundException: http://localhost:9090/WS/sayHello?wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(Unknown Source)
... 8 more

SO here we finish our web services.




 
 
 

2 comments:

Anonymous said...

Online Casino no Deposit Bonus Codes 2021 | CasinoWow
Best online casino with bonus code no deposit is at -125, no deposit spins no deposit, spins on roulette and 코인카지노 가입코드 a wide variety of other casino games with the

ebbajaegers said...

How to get to Mohegan Sun by Bus - Dr.CD
Directions to 순천 출장샵 Mohegan Sun (Station 3) with public transportation. The 태백 출장샵 following 상주 출장샵 transit lines have routes that pass near Mohegan 순천 출장안마 Sun Arena. 의왕 출장안마

Junit 5 Architecture

Modules in JUnit 5 : •         junit-jupiter-api: This module defines the API that you need to write your tests. •         ...