How to integrate Web-service in existing Web project?
I am new to Web-Services, I want to integrate Web-ser开发者_如何学编程vice in my existing web project, but I am confused how to do it. Can any one tell me how to do it?
For my project I am using:
- Spring Framework
- Hibernate
- Pojo based (not using ant builder, hibernate.cfg.xml)
- Eclipse
Suppose I have SecurityDepositServiceImpl class that contain getTenderTitleForSecurityDeposit() function, then how I do web-service for that?
A quick way to implement a web service is the following one:
Be sure that the following jars will be available to your webapp: jaxws-rt.jar; jaxb-impl.jar; streambuffer.jar; policy.jar; jaxws-api.jar; stax-ex.jar; gmbal-api-only.jar; management-api.jar
Create a "sun-jaxws.xml" file on your WEB-INF folder and fill it with the following:
<?xml version="1.0" encoding="UTF-8"?> <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> <endpoint implementation="com.foo.ws.MyWebServiceClass" name="MyServices" url-pattern="/ws/theUrlILikeTheMost" /> </endpoints>
- Add in your web.xml the following snippet:
<listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <servlet-name>WebServices</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WebServices</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
- Create your class com.foo.ws.MyWebServiceClass and annotate what you like to provide:
@WebService(serviceName="MyServices") public class MyWebServiceClass extends SpringBeanAutowiringSupport { @WebMethod public List<String> myMethodExposed(String username) { List<String> toret = new ArrayList<String>(); toret.add("Hello world"); toret.add("Life id beautiful"); return toret; } }
et voila: your web service is ready.
精彩评论