学习网址:Java6 WebService的发布 ; java 调用webservice的各种方法总结
模仿实例,遇见问题记录
1、eclipse建立Dynamic Web Project项目
2、编写webService服务
- package lavasoft.server;
-
- import javax.jws.WebService;
-
-
-
- @WebService
- public class JavaWebService {
-
-
-
-
- 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;
-
-
-
-
- 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服务");
-
- 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>
注意:
<load-on-startup>1</load-on-startup>,这样写的目的是Servlet能伴随容器的启动在第一时间加载。以便服务能尽可能早的发布,否则,只有在第一次请求该Servlet的时候,才会被加载到容器。
web.xml配置与java文件所在包要一致
发布端口与tomcat端口最好不要重复
5、初始页index.jsp
- <%@ 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();
- }
- }
- }
运行看是否可以成功访问? 成功即可
问题记录:
访问时的传参我还有点问题,没有运行成功