File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
khan gul
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic