wood burning stoves 2.0*
The moose likes Web Services and the fly likes can we annotate @WebMethod on a static method in web service? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "can we annotate @WebMethod on a static method in web service?" Watch "can we annotate @WebMethod on a static method in web service?" New topic
Author

can we annotate @WebMethod on a static method in web service?

khan gul
Ranch Hand

Joined: Jan 27, 2011
Posts: 30
Hi,
My question is can we annotate a static method with @WebMethod for example:

@WebService
public class Hello {

@WebMethod
public static String sayHello(String name){

return "hello " + name;
}

}

If this is not possible then why?
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

khan gul wrote:Hi,
My question is can we annotate a static method with @WebMethod for example:

@WebService
public class Hello {

@WebMethod
public static String sayHello(String name){

return "hello " + name;
}

}

If this is not possible then why?


No, you can't use @WebMethod on static methods. The reason is better explained here


Regards
KumarRaja

khan gul
Ranch Hand

Joined: Jan 27, 2011
Posts: 30
Thanks kumar,
But I am able to publish the above class successfully and I am able to generate soap request using SOAP UI and recieved the reply. Still not sure why we cannot use static method.

regards.
khan
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
That you succeeds in one case means nothing if you want your code to be portable.
If you go against the specification, then there is nothing that guarantees that the code will work with another web service stack or a newer version of the same web service stack.
However, there is nothing stopping you, or anyone else, from doing whatever they feel like.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
khan gul
Ranch Hand

Joined: Jan 27, 2011
Posts: 30
Thanks Ivan,
Just for the argument of understanding, how static method will cause problem especially if its thread safe? As in the above example, the method is thread safe so in my opinion it even better to have a static method rather than non static method.
I would appreciate if you can elaborate what type of problem this may cause if we have static method with @WebMethod annotation.
regards,
khan
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

khan gul wrote:Thanks Ivan,
Just for the argument of understanding, how static method will cause problem especially if its thread safe? As in the above example, the method is thread safe so in my opinion it even better to have a static method rather than non static method.
I would appreciate if you can elaborate what type of problem this may cause if we have static method with @WebMethod annotation.
regards,
khan


Hi Khan,

What are the reason you feel that it is better to have static methods rather than non static methods. Could you please elaborate on this?

Now here is my understanding, specs advise to use non static methods specially for the container managed objects like ejbs, servlets, webservice components etc, because a run time object is provided by the container which would extend the components we as a developer develops. Now these run time objects include some additional features and functionalities specific to that container and were not handled by the components we design. In order to not to interfere with container, specs do not recommend to use static methods.

As you might be aware of overriding vs hiding, which was also clearly explained here, we will not be able to utilize the polymorphism, which is inherently used at run time. We invoke the methods on the interface, but at run time container delegates the invocation to the run time object it created and also which extends the components we designed.

So, that is one of the primary reasons, why static methods are not recommended. What do you think about this ?
khan gul
Ranch Hand

Joined: Jan 27, 2011
Posts: 30
Thanks alot Kumar for explaining in details. I was not thinking something else. Indeed static method will cause lot of issue.
regards,
khan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we annotate @WebMethod on a static method in web service?
 
Similar Threads
WebParam annotation.
Simple JBoss Web Service With Eclipse
calling a .net web service using j2me
Return type of web service..
Question about expose overloaded session bean method as web service.