学习网址:Java6 WebService的发布 ; java 调用webservice的各种方法总结
模仿实例,遇见问题记录
1、eclipse建立Dynamic Web Project项目 2、编写webService服务
package lavasoft.server; import javax.jws.WebService; /** * Java开发的WebService服务端 */ @WebService public class JavaWebService { /** * Web服务中的业务方法 * @return 一个字符串 */ public String doSomething(String username) { return username + " hello!"; } public String doHello() { return "XFire hello!"; } }
3、开发发布服务的Servlet,为了通用性,最好选择GenericServlet来继承
package lavasoft.servlet; import lavasoft.server.JavaWebService; import javax.servlet.*; import javax.xml.ws.Endpoint; import java.io.IOException; /** * 发布Java WebService的Servlet */ public class WSServlet extends GenericServlet { private static final long serialVersionUID = 1L; @Override public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); System.out.println("准备启动WebService服务"); //发布一个WebService Endpoint.publish("http://localhost:8888/javaws/JavaWebService", new JavaWebService()); System.out.println("已成功启动WebService服务"); } public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("此Servlet不处理任何业务逻辑,仅仅用来发布一个Web服务"); } }
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>WSServlet</servlet-name> <servlet-class>lavasoft.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WSServlet</servlet-name> <url-pattern>/servlet/WSServlet</url-pattern> </servlet-mapping> </web-app>
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>展示</title> </head> <body> <div> <h1>这是一个测试java WebService发布服务的例子</h1> <a href="http://localhost:8888/javaws/JavaWebService">点此处访问WSDL</a> </div> </body> </html>
6、启动Tocmat 浏览器中输入:http://localhost/JavaWebService/ 成功访问即可
(我的tomcat没有端口)
7、客户端调用
建立一个简单的空项目,一个类
import java.net.URL; import org.codehaus.xfire.client.Client; public class XFireTest { public static void main(String[] args) { try { Client c = new Client(new URL( "http://localhost:8888/javaws/JavaWebService?wsdl")); Object[] results = c.invoke("doHello", new Object[0]); System.out.println(results.length); for (Object one : results) { System.out.println("1 结果是:" + one); } } catch (Exception e) { e.printStackTrace(); } } }
运行看是否可以成功访问? 成功即可
问题记录:
访问时的传参我还有点问题,没有运行成功
上一篇:【WebService学习过程记录】照猫画虎---运行示例(一)