• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
khan gul
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
khan gul
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Kumar for explaining in details. I was not thinking something else. Indeed static method will cause lot of issue.
regards,
khan
reply
    Bookmark Topic Watch Topic
  • New Topic