I used Axis2 to create a web services. And it was very easy to write, test and run it.
But now I have existing Java Web App running under Tomcat and I want to add Web Services to it. E.g. instead to process POST and GET parameters and generate HTML I want to receive SOAP messages, to process it and to return response as SOAP message again.
Which technology/framework/library/etc. should use?
Preferably should be easy as writing Web Services开发者_StackOverflow中文版 for Axis2.
One possible workflow is:
- Refactor your web app so that the business logic is implemented in separate methods, and
doPost()
anddoGet()
only call these methods. - Extract an interface of your business methods from the web app class.
- Run
java2wsdl
to convert the above interface and its related types into a WSDL spec. - Run
wsdl2java
with server-side bindings to generate the skeleton class and other auxiliaries. - Replace the skeleton class with the original web app class (or copy the relevant methods from the web app to the skeleton class).
- Remove the redundant
doPost()
anddoGet()
methods, delete the old web app.
Et voila! You have an Axis2/Tomcat web service. It requires some refactoring, but no change to the core logic.
- Add the
@WebService
annotation to your java class. - add
@WebMethod
to the methods you want to expose as web-service operations. - Add the framework specific servlet and filter mapping in the web.xml file. For example, for the Sun RI f/w it is
WSServlet
. There must be something similar for Axis too. - Write a WSDL file, or generate it from the Java class you have. You can use a tool like wsgen [works for Sun RI]. it's java2wsdl for Axis.
- Add the required web descriptor file. It's usually a
wsdd
file for Axis and thesun-jaxws.xml
file, in case of Sun RI.
If you want add a new web service you can add that to the existing web app using Axis 2
If you want to use an existing feature(functionality), first you need to refactor your web app and extract the business logic in to new methods. Then annotate the class and interface with
@WebService
. If you want to hide some methods which expose in web service, then annotate those methods with@WebMethod(exclude=true)
精彩评论