使用xFire和Spring构建WebService

首先感谢阿蜜果,多谢其写的文章,本文几乎完全参考自其文。

 

使用xFireSpring开发WebService

要下载xFireSpring,我用的xFire1.2.6版,不要只下载xFire这一个jar,下载zip的发布版,这样其他依赖的jar包就都包含了。Spring我用的是2.5.6版的。Eclipse我用的3.5的含J2EE的。

先构建Server端,新建Dynamic Web Project,将以下jar包加入到lib中:activation-1.1.jarcommons-codec-1.3.jarcommons-httpclient-3.0.jarcommons-logging-1.0.4.jarjaxen-1.1-beta-9.jarjdom-1.0.jarmail-1.4.jarservlet-api-2.3.jarstax-api-1.0.1.jarwsdl4j-1.6.1.jarxfire-all-1.2.6.jarXmlSchema-1.1.jar以及spring-2.5.6.jarspring-webmvc.jar

配置web.xml,内容如下:

  WebServiceServer

 

 

    index.jsp

 

 

  

       contextConfigLocation

       /WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml

   

   

   

       org.springframework.web.context.ContextLoaderListener

      

   

   

    

       org.springframework.web.util.IntrospectorCleanupListener

      

   

 

         

       xfire  

       org.springframework.web.servlet.DispatcherServlet

   

     

   

       xfire

       *.ws

   

   

   

      

       xfireServlet

       org.codehaus.xfire.spring.XFireSpringServlet

   

   

   

       xfireServlet

      

       /service/*

          

文件中已指出SpringxFire的配置文件位置,这两个配置文件如下:

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 mapUser =

                   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"%>

Server

         <%response.sendRedirect("userService.ws?wsdl"); %>

现在发布应用,就可以看到相应的wsdl文件了。

编写客户端,导入同样的jar文件,只是额外多一个对刚建的server打的jar包。web.xml配置文件如下:

  WebServiceClientWeb 

 

    index.jsp

   

  

       contextConfigLocation

       /WEB-INF/client.xml

    

   

       org.springframework.web.context.ContextLoaderListener

      

   

   

      

       test  

       com.viewsky.ws.servlet.TestUserServiceServlet

      

   

   

       test

       /test

   

对应的Spring配置文件为client.xml,内容如下:

    "http://www.springframework.org/dtd/spring-beans.dtd">

 

   

                      

                            com.viewsky.service.IUserService      

          

          

                

                            http://10.227.1.39:8080/WebServiceServer/userService.ws?wsdl      

            

    

现在只需要一个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提供的服务了。需要特别说明的是,在我自己的机子上,同时发布ServerCient是不成功的,只能把ServerClient部署到不同的机器上,这样就可以看到效果了。

 

你可能感兴趣的:(Java,webservice,spring,string,encoding,user,servlet)