论坛首页 入门技术论坛

求教XFire返回自定义对象的问题

浏览 4601 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-08-21  
User类:
public class User {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public interface UserService {
     public User getUser();
}

实现类:
public class UserServiceImpl implements UserService {

public User getUser(){
User u = new User();
u.setName("test");
return u;
}

}

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" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <servlet>
   <servlet-name>XFireServlet</servlet-name>
   <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
      
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
    <!-- base service -->
    <bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false"
     abstract="true">
    <property name="serviceFactory" ref="xfire.serviceFactory" />
        <property name="xfire" ref="xfire" />
        <property name="scope" value="application"/>
        <property name="use" value="literal"/>
        <property name="style" value="wrapped"/>      
    </bean>  
         
    <bean id="userService" parent="webService">   
        <property name="serviceBean" ref="userServiceImpl" />
        <property name="serviceClass" value="UserService" />
        <property name="name" value="UserService"/> 
    </bean>
   
    <bean id="userServiceImpl" class="UserServiceImpl"/>
</beans>


客户端调用代码:
public static void main(String[] args) {

Service srvcModel = new ObjectServiceFactory()
.create(UserService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String url= "http://localhost:8080/ws/services/UserService";
try {
UserService srvc = (UserService) factory.create(
srvcModel, url);
User u= srvc.getUser();
System.out.println(u.getName());
} catch (MalformedURLException e) {
e.printStackTrace();
}

}

打印出来的结果为:null
理论上来说USER类里面,只有String这样的基本类型,不要什么映射就可以了,但是还得不到值,百思不得其解,期待那个高*手指点指点....多谢!!!
   发表时间:2007-08-22  
这么问题试了好多次,终于知道问题出现在那里了,就是在定义User类的时候,服务端的包名一定要和客户端的包名一样,不然的话,返回的值就为null了.具体不知道为什么要这么做才行,还需要继续的学习和研究...
1 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics