1

I'm learning about web services, and saw various examples. Some of them used @WebService and/or @WebMethod annotations while others don't. My question is; is it necessary to use these annotations, what is the drawback of not using?

2 Answers 2

1

You should start by learning a bit about what annotations are. A useful starting point is the Oracle tutorial at http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html; an incomplete (but useful) summary is that annotations add metadata to the class file that can be examined at runtime.

Those annotations are for use by a an EJB container. The annotation tells the container that a method is to be exposed as a web service, and the container automagically exposes them as such, with a little additional configuration. The Spring container has its own set of annotations that accomplish the same thing.

There's no reason you can't skip the annotations and write web services from scratch as simple servlets, but it's usually cleaner, faster, and more useful to just focus on the business logic and let a container do the work of translating your code into a service for you.

1
  • 1
    Annotations can be used in J2SE too. No EJB container required.
    – szhem
    Nov 15, 2011 at 4:33
0

Only usage of @WebService annotation is necessary. They are required for more flexibility when developing web services. Just look at javadocs of @WebService and @WebMethod. They have a lot of attributes which define how your service will be exposed because as a rule they are used to generate WSDL-file, so you are able to customize its different parts. Generally, I'd prefer to use annotation to have a higher flexibility with configuring how my service will look like for its clients. So these annotations are used to define and customize a contract between your service and its clients.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.