How to utilize web service client generated from WSDL? - java

I'm trying to write a simple web service client to interact with my simple web service which only returns a user id that's passed in. So I created a web service client in eclipse and generated a few files for me; wsCall, wsCallBindingStub, wsCallProxy, wsCallService, wsCallServiceLocator. The stub is the conly class I found that has my web service methods in it, because my ws is simple at this stage?
So I want to invoke the call, what do I need to make the call?
I've seen all the examples online have the try-catch for a remote exception or Axis fault, then the classes are instantiated (including a response class, to deserialize?) and make the ws call via the stub class. Is that all I need to call for my case?
wsCallBindingStub stub = new wsCallBindingStub();
String retString = stub.sayHi(1); // 1: my user id
return retString;
Thank you!

Ahh I figured it out, I was getting an error because my wsdl uses the hostname and I needed to specify the ip.. as for the code needed it was pretty much identicle;
wsCall ws = new wsCallServiceLocator().getWsCallPort();
result = ws.sayHi(x);

Related

There is no stub implementation for the interface error while creating stub

I am new to web service .
I have to consume the RPC XML SOAP web service , i just tried to create the stubs and locators through the wsdl provided by the web service provider .
Now when i am creating the object of XXXstub class in order to call the service method . i get Exception in thread "main" javax.xml.rpc.ServiceException: There is no stub implementation for the interface.
below is the code where i am trying to create the locator and stub object to call service method.
final EDWebService_ServiceLocator locator=new EDWebService_ServiceLocator();
final EDWebServiceSoapBindingStub stub=(EDWebServiceSoapBindingStub) locator.getPort(EDWebService_Service.class);
checked further it goes in getPort() and stuck in the if condition of the method.
Any help/clue related to this is much appreciated.
Thanks
You will have to provide the WSDL location as well.
Since, the implementation(that is, the original web service) is located remotely (which you access using the WSDL), you need to provide this reference to identify the location where Web service is hosted.
I guess, you will have a getPort() method that accepts the url to WSDL. Please use that method.

Restlet add parameter to webservice call

I cannot seem to find anything on the subject on the site of restlet or anywhere on how to add parameters to a webservice call.
I am able to use restlet to call a webservice that doesn't need any parameters and handle the respond.
If you know any other frameworks that are able to call a webservice and pass parameters(rest) please tell me.
Thanks in advance.
To add a query parameter to a webservice call with Restlet (version 2.3) you can do the following:
// Create a client resource which will call a given service
ClientResource clientResource = new ClientResource("http://serviceToCall.com");
// Add a query parameter
clientResource.addQueryParameter("queryParameterName", "queryParameterValue");
// Make a "GET"
clientResource.get();
Here is the corresponding Javadocs.
Give this a try. Based on the example from the Restlet wiki:
// Create the client resource
ClientResource resource = new ClientResource("http://www.restlet.org");
// Add a query parameter equiv to ?A=1
resource.getReference().addQueryParameter("A","1");
// Customize the referrer property
resource.setReferrerRef("http://www.mysite.org");
// Write the response entity on the console
resource.get().write(System.out);

WebService Client in Java

I have the following problem: I am completely new to java EE (know only about servlets and JSPs) and especially web services.
I need to develop a client for a web service (it needs to query the service for useful information once in a minute).
In my mind this client would be a simple java-SWing-based program, which would query the web service through simple Socket when the application client runs. How can that be done?
Is it possible to do in that way? If not, which is the easiest way to create such a client?
I would suggest using Apache CXF. Simple and powerful framework.
And yes, that is possible to implement what you said using this framework. Just read tutorials and play around for a bit with it.
Inorder to connect to a web service using a java client follow the below mentioned steps:
1. Get the URL in which the webservice is hosted. This is usually of the fomat http://<IP_OF_SERVER>:<PORT_OF_SERVER>/<WEB_APP_NAME>?wsdl
2. Get the qualified name of the service:
// 1st arg is the service URI
// 2nd is the service name published in the WSDL
QName qname = new QName(<Service_URI>, <SERVICE_NAME_PUBLISHED_WSDL>);<br/>
3. Create a factory for the service:
Service service = Service.create(url, qname);
4. Extract the endpoint interface, the service "port":
<Port_Class_Name> eif = service.getPort(<Port_Class_Name>);
5. Now use the methods on the Port, which are the actual methods in your webservice.
You might want to try REST Webservice, try Jersey REST (or others). With rest you can connect it with http connection (GET and POST).
Inorder to connect to a web service using a java client follow the below mentioned steps:
1.URL wsdlUrl = new URL("your wsdl url);
2.QName qname = new QName("targetNamespace in ur wsdl file","service name in your wsdl file");
Service service = Services.create(wsdlUrl,qname);
4.suppose getData() is your SEI
GetData data = (GetData)service.getPort(GetData.class);
5.call the your methods using data object
ex:data.getId(String name); this will return your response

How to implement Soap Faults in Java webservices?

Am pretty new to web services and have been trying to implement Soap Faults. I used Apache Axis2 to generate webservice in the following manner.
public interface XYZ{
public String myMethod(User[] user)
}
Here I have created a User class with some variables so that I can generate User object at .Net environment to pass User[] of objects.
Public class Webservice implements XYZ
{
Public String myMethod(User[] user){
//My implementation
}
}
Now, I created a dynamic project using Eclipse and with the help of Axis2 plugin I created webservice for my "Webservice" class which generates wsdl file. I deployed the webcontent in the Tomcat folder and able to access the WSDL file in the .Net environment. I am able to pass array of objects (User[]) from .Net to Java and able to do my task. Now, I need to implement Soap Faults in Java which I am not sure how to implement.
Can anyone help me with an example or tutorial ?
Your best bet is to Google for something like "jax-ws faults". For example:
http://www.ibm.com/developerworks/webservices/library/ws-jaxws-faults/index.html
You can also implement an error handler, as discussed under "Using handlers in JAX-WS Web services" here:
http://axis.apache.org/axis2/java/core/docs/jaxws-guide.html#BottomUpService
Most frameworks will trigger a SOAP fault when you throw an Exception in the method implementing your operation. That will not give you much control on the SOAP fault content though.
See here for some details on Axis
Generally, You don't need any specific coding for implementing SOAP fault.. Whenever there is any exception thrown by the method (here myMethod in your example.), axis will automatically generate SOAPFault element in the resulting response. The exception is actually wrapped into AxisFault exception and sent to the client.
See here a i.

Java Web Service - Secure web methods by user

Java web service, is it possible to expose different methods to different user?
For example, I have 10 methods in my web service but I want to allow user A access to 1 or 2 methods only, how can this be done?
I guess you can not completely hide the methods from the user. The only thing you can do is to provide only required information to the specific User. In one my application I have implemented this by using Decorate Design Pattern. I will try to explain it.
You can separate this logic in some non-webservice class. Create 2 web services (one for each userAccess Model). Call the separated logic from each of the web service.
Say you have created class CommonA which contains methods 1 ~10. Create web service say ForUserA this contains method 1 and 2 only which calls method 1 and 2 of CommonA. and so on.
It will be a great pleasure if anyone suggest the better way to do this.
In order for your webservice to determine which user is currently calling your webservice method, you need some kind of authentication.
Since both SAOP & REST use HTTP protocol, you can use sessions.
Once your client has authenticated himself, you can allow/deny him access to any webmethod you like.
Here is an easy example for a SOAP service.
#Resource WebServiceContext wsContext;
MessageContext mc = wsContext.getMessageContext();
HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
String username = (String)session.getAttribute("username");
if(username.equals("userA") {
// Do your thing
} else {
throw new WebServiceException("Not allowed to access this method.");
}

Categories