开发者

Expose Java class as web service in existing Web App running under Tomcat

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:

  1. Refactor your web app so that the business logic is implemented in separate methods, and doPost() and doGet() only call these methods.
  2. Extract an interface of your business methods from the web app class.
  3. Run java2wsdl to convert the above interface and its related types into a WSDL spec.
  4. Run wsdl2java with server-side bindings to generate the skeleton class and other auxiliaries.
  5. Replace the skeleton class with the original web app class (or copy the relevant methods from the web app to the skeleton class).
  6. Remove the redundant doPost() and doGet() 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.


  1. Add the @WebService annotation to your java class.
  2. add @WebMethod to the methods you want to expose as web-service operations.
  3. 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.
  4. 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.
  5. Add the required web descriptor file. It's usually a wsdd file for Axis and the sun-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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜