首先感谢阿蜜果,多谢其写的文章,本文几乎完全参考自其文。
使用xFire和Spring开发WebService
要下载xFire和Spring,我用的xFire为1.2.6版,不要只下载xFire这一个jar,下载zip的发布版,这样其他依赖的jar包就都包含了。Spring我用的是2.5.6版的。Eclipse我用的3.5的含J2EE的。
先构建Server端,新建Dynamic Web Project,将以下jar包加入到lib中:activation-1.1.jar、commons-codec-1.3.jar、commons-httpclient-3.0.jar、commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jdom-1.0.jar、mail-1.4.jar、servlet-api-2.3.jar、stax-api-1.0.1.jar、wsdl4j-1.6.1.jar、xfire-all-1.2.6.jar、XmlSchema-1.1.jar以及spring-2.5.6.jar和spring-webmvc.jar。
配置web.xml,内容如下:
文件中已指出Spring和xFire的配置文件位置,这两个配置文件如下:
applicationContext.xml
"http://www.springframework.org/dtd/spring-beans.dtd">
*******************************************************************************
xFire-servlet.xml
"http://www.springframework.org/dtd/spring-beans.dtd">
配置文件写好了,需要的类如下:
User.java
package com.viewsky.bean;
public class User
{
private String username;
private int age;
private String hobby;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getHobby()
{
return hobby;
}
public void setHobby(String hobby)
{
this.hobby = hobby;
}
public String toString()
{
return "用户:" + username +
",年龄" + age +
"时,爱好:" + hobby;
}
}
*******************************************************************************
IuserService.java
package com.viewsky.service;
import com.viewsky.bean.User;
public interface IUserService
{
public abstract User findUserHobby(User user) throws Exception;
}
*******************************************************************************
UserServiceImpl.java
package com.viewsky.service;
import java.util.HashMap;
import java.util.Map;
import com.viewsky.bean.User;
public class UserServiceImpl implements IUserService
{
private static final Map
new HashMap
static
{
mapUser.put("jg.sun", "篮球");
mapUser.put("lcrystal", "足球");
mapUser.put("s0meb0dy", "游泳");
mapUser.put("猫来猫去", "睡觉");
mapUser.put("小刚", "唱歌");
}
public User findUserHobby(User user) throws Exception
{
if(user == null)
{
return null;
}
String hobby = mapUser.get(user.getUsername());
if(hobby == null)
{
user.setHobby("无");
}
else
{
user.setHobby(hobby);
}
return user;
}
}
一切都好了,只差一个index.jsp了,其代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%response.sendRedirect("userService.ws?wsdl"); %>
现在发布应用,就可以看到相应的wsdl文件了。
编写客户端,导入同样的jar文件,只是额外多一个对刚建的server打的jar包。web.xml配置文件如下:
对应的Spring配置文件为client.xml,内容如下:
"http://www.springframework.org/dtd/spring-beans.dtd">
现在只需要一个servlet就一切OK了,TestUserServiceServlet.java内容如下:
package com.viewsky.ws.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.viewsky.bean.User;
import com.viewsky.service.IUserService;
public class TestUserServiceServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setCharacterEncoding("GBK");
ServletContext application;
WebApplicationContext wac;
application = getServletContext();
wac = WebApplicationContextUtils.getWebApplicationContext(application);
IUserService userService = (IUserService) wac.getBean("testWebService");
User user = new User();
user.setUsername("lcrystal");
user.setAge(27);
try
{
user = userService.findUserHobby(user);
}
catch(Exception e)
{
e.printStackTrace();
}
PrintWriter out = resp.getWriter();
out.println(user);
}
}
发布客户端就可以看到利用WebService提供的服务了。需要特别说明的是,在我自己的机子上,同时发布Server和Cient是不成功的,只能把Server和Client部署到不同的机器上,这样就可以看到效果了。