大家好,最近实现一个项目需要用到Hessian,麻烦用过Hessian的朋友能帮我看看为什么测试服务调不出服务器端的实体类
首先是客户端,创建实体Bean
public class PosInfo implements java.io.Serializable { private static final long serialVersionUID = 4791943343138808983L; private String posid; private String posname; private String lmodifydate; public String getPosid() { return posid; } public void setPosid(String posid) { this.posid = posid; } public String getPosname() { return posname; } public void setPosname(String posname) { this.posname = posname; } public String getLmodifydate() { return lmodifydate; } public void setLmodifydate(String lmodifydate) { this.lmodifydate = lmodifydate; } }
接下来创建Hessian服务的接口
public interface IPosService { public void savePosition(PosVO vo); public void updatePosition(PosVO vo); public PosInfo queryPositionById(String posId); public void deletePosition(String posId); public List queryPositions(); public String test(String s); public List find(String arg0); }
接下来是实现了接口的服务类
public class PosServiceImpl implements IPosService { private static final Log log = LogFactory.getLog(PosServiceImpl.class); // private BaseDAO dao; // // // public BaseDAO getDao() { // return dao; // } // // public void setDao(BaseDAO dao) { // this.dao = dao; // } // BeanFactory factory = new ClassPathXmlApplicationContext( // "classpath:applicationContext.xml"); // PosDAO dao = (PosDAO) factory.getBean("posDAO"); // // ApplicationContext context = new ClassPathXmlApplicationContext( // "classpath:application-position.xml"); // PosDAO dao = (PosDAO) context.getBean("posDAO"); // private PosDAO dao; public PosDAO getDao() { return dao; } public void setDao(PosDAO dao) { this.dao = dao; } private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List find(String arg0) { List list = dao.find(arg0); return list; } public void savePosition(PosVO vo) { log.info("add new instance"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); PosInfo pos = new PosInfo(); pos.setPosname(vo.getPosname()); pos.setLmodifydate(sdf.format(new Date()).toString()); log.info("now begin save"); dao.save(pos); log.info("add success !"); } public void updatePosition(PosVO vo) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); PosInfo pos = this.queryPositionById(vo.getPosid()); pos.setPosname(vo.getPosname()); pos.setLmodifydate(sdf.format(new Date()).toString()); dao.update(pos); } public PosInfo queryPositionById(String posId) { return (PosInfo) dao.get(PosInfo.class, posId); } public void deletePosition(String posId) { PosInfo pos = this.queryPositionById(posId); dao.delete(pos); } public List queryPositions() { List poses = dao.find("from PosInfo po order by po.lmodifydate desc"); return poses; } public String test(String s) { log.info("===========进入service层"); return dao.test(s); } }
还有service层引用的DAO层
public class PosDAO extends BaseDAO { private static final Log log = LogFactory.getLog(PosDAO.class); protected void initDao() { // do nothing } public void delete(Object arg0) throws DataAccessException { // TODO Auto-generated method stub super.delete(arg0); } public List find(String arg0) throws DataAccessException { // TODO Auto-generated method stub return super.find(arg0); } public Object get(Class arg0, Serializable arg1) throws ObjectRetrievalFailureException, DataAccessException { // TODO Auto-generated method stub return super.get(arg0, arg1); } public void save(Object arg0) throws DataAccessException { // TODO Auto-generated method stub super.save(arg0); } public void update(Object arg0) throws DataAccessException { // TODO Auto-generated method stub super.update(arg0); } public List<PosInfo> getAllPosInfo() { return (List<PosInfo>) super.find("from PosInfo"); } public String test(String s) { log.info("hessian测试"); return s+"成功"; } }
spring 的配置文件在此省略,没什么好说的了
然后是web.xml的配置
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> <!-- com.caucho.hessian.server.HessianServlet --> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- <init-param> <param-name>service-class</param-name> <param-value> com.*.*.*.service.impl.PosServiceImpl </param-value> </init-param>--> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping>
还要配置dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.sybase.jdbc2.jdbc.SybDataSource</value> </property> <property name="url"> <value>jdbc:sybase:Tds:****</value> </property> <property name="username"> <value>****</value> </property> <property name="password"> <value>****</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SybaseDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <value> classpath:/com/*/*/*/domain/ </value> </list> </property> </bean> <bean id="dao" class="com.*.*.*.dao.PosDAO"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="iPosService" class="com.*.*.*.service.impl.PosServiceImpl"> <property name="dao"> <ref bean="dao" /> </property> </bean> <bean name="/HessianService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="iPosService"></property> <property name="serviceInterface" value="com.*.*.*.service.IPosService" /> </bean> </beans>