java webservice简单实现
- 接口(SayHello.java)
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface SayHello {
@WebMethod
public String sayHello(String name);
}
- 接口实现(SayHelloImpl.java)
import javax.jws.WebService;
@WebService(endpointInterface="com.webservice.service.SayHello")
public class SayHelloImpl implements SayHello {
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
return name + " Hello";
}
}
- 部署
在WEB-INF目录下建立sun-jaxws.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="SayHello" implementation="com.webservice.service.SayHelloImpl"
url-pattern="/webservice/sayhello" />
</endpoints>
其中的url-pattern即为webservice的映射地址
- web配置
在web.xml中添加如下监听器:
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
部署到tomcat中,打开http://localhost:8080/Ada/webservice/sayhello即可得到webservice服务列表,进入http://localhost:8080/Ada/webservice/sayhello?wsdl即可看到wsdl文档