为XFire生成的WebService客户端设置连接超时参数
由于要从另一个系统的WebService读取大量的数据,因此要修改连接WebService的超时参数。WebService的客户端代码是通过XFire的Ant任务生成的。Google一下,没找到答案,但最后还是在XFire官方找到了答案。
假设生成的客户端代码中的服务接口和Client的类分别为TestService和TestServiceClient,修改连接超时参数代码如下:
//创建Service对象
TestServiceClient testClient = new TestServiceClient();TestService testService = testClient.getTestServicePort();
//设置连接参数
HttpClientParams params = new HttpClientParams();params.setParameter(HttpClientParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
params.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, setting.getTimeout() * 1000);//单位是毫秒
Client client = Client.getInstance(testService);
client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params);
XFire是基于Apache的HttpClient,所以实际上连接超时的参数是就是设置在HttpClient上的。
把MyEclipse项目转为WTP项目
马上要进入另一个项目组了,新项目所用的IDE是MyEclipse。我已不习惯了庞大的MyEclipse,而习惯了WTP并且想试验一下新的Eclipse3.5,因此需要把MyEclipse项目转换为WTP项目。(PS:不遵守团队开发规范是不正确的行为!)
1.在Eclipse里导入MyEclipse项目到工作空间。
2.编辑.project文件,在<natures></natures>中加入
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
在<buildSpec></buildSpec>节点加入
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
3.在Eclipse中刷新项目。
4.右击项目–>Properties—>Project Facets,在右边勾选Java与Dynamic Web Module,应用。
5.如果你的项目源代码文件夹不是WTP默认的src,则需要到Java Build Path中去添加相应源代码文件夹。
6.如果你的项目Web文件夹不是WTP默认的WebContent,则需要文件文件 <项目目录>/.settings/org.eclipse.wst.common.component,找到
<wb-resource deploy-path="/" source-path="/WebContent"/>
并修改,如
<wb-resource deploy-path="/" source-path="/webapp"/>
当BeanUtils的拷贝遇上java.util.Date
BeanUtils是apache基金会下的一个开源项目,主要用于对Java Bean的操作。在一个项目中,经常要用到Bean属性拷贝,使用了org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orgi)。由于此方法声明了抛出异常,在项目中为了方便,便对此作了简单封装,把异常处理了:
public static void copy(Object dest, Object orgi) {
try {
BeanUtils.copyProperties(dest, orgi);
} catch (Exception e) {
log.error("Bean属性拷贝出错。", e);
}
}
后来却发现,当源对象orgi中某个java.util.Date类型的属性为null时,copy会失败(虽然异常被处理了),目标对象dest中部分非Date类型的属性也为空,虽然源对象中这些属性是有值。原因就是上BeanUtils在处理属性值为null的Date类型时出错了,便抛出了异常,后面的属性便不会再复制。
后来在JavaEye搜到了答案:为BeanUtils注册一个日期类型转换器:使用org.apache.commons.beanutils.ConvertUtils,注册org.apache.commons.beanutils.converters.DateConverter。下面的代码是放在copy方法所在的类:
static{
ConvertUtils.register(new DateConverter(), Date.class);
}
基于注解与自动扫描的某些实体没被映射的原因之一
在尝试修改基于SpringSide 3.1.3的mini-web过程中,又遇到了一个奇怪的问题。一个hql如下:
StringBuilder hql = new StringBuilder();
hql.append("SELECT new ");
hql.append(CommentBean.class.getName());
hql.append(" (o,u.name,u.image) ");
hql.append(" FROM ");
hql.append(Comment.class.getSimpleName()).append(" o, ");
hql.append(User.class.getSimpleName()).append(" u ");
hql.append(" WHERE o.userId=u.id AND o.infoId=? ");
hql.append(" ORDER BY o.commentTime DESC");
Comment这个实体类的全限定名是:org.shoopman.entity.Comment
但在运行时却报错:
Comment is not mapped [SELECT new org.shoopman.service.dto.CommentBean (o,u.name,u.image) FROM Comment o, org.shoopman.entity.user.User u WHERE o.userId=u.id AND o.infoId=? ORDER BY o.commentTime DESC]
org.hibernate.hql.ast.QuerySyntaxException: Comment is not mapped [SELECT new org.shoopman.service.dto.CommentBean (o,u.name,u.image) FROM Comment o, org.shoopman.entity.user.User u WHERE o.userId=u.id AND o.infoId=? ORDER BY o.commentTime DESC]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
而其他实体的查询却没有问题。先看一下spring的applicationContext.xml关于Hibernate的配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
</props>
</property>
<property name="packagesToScan" value="org.shoopman.entity.*" />
</bean>
问题就在这个自动描述packagesToScan的配置上。如上的配置,Spring只会扫描到org.shoopman.entity这个包下的子包里的实体类,而直接放在这个包下的实体却没有描述。
无奈,只好在新建一个org.shoopman.entity.common包,把Comment移到此包下。到此,问题解决。
题外话:看上面的hql生成过程,在使用User这个实体类时,我用的是User.class.getSimpleName(),为什么Hibernate在生成hql却是全限定名。当把Comment移到common包下,控制台里打印出来的hql又是SimpleName。这是Hibernate的一个容错处理吗?
SessionListener与Spring
在SessionListener里,需要使用被Spring管理起来的其他Bean,如某些DAO或者Manager、Service。在实现这个过程是这样的:
1.把SessionListener也交给Spring来管理。
由于使用了Spring Annotation,所以在SessionListener上加了@Service标记,并且把需要用到的其他Bean在SessionListener中定义为成员变量并加上@Autowried。但结果是失败的,SessionListener里的其他Bean没有被注入。
2.直接通过Spring的Context来获得相应的DAO等的实例。
Spring Annotation默认是ByType的方式来注入Bean,因此写了一个静态方法来获取相关的Bean,不知道有没有其他更好的办法,请告诉我吧:
/**
* 根据类型从Spring中取得Bean
*
* @param <T> Bean的类型
* @param beanClass Bean的Class对象
* @param servletContext
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getSpringBean(Class<T> beanClass,
ServletContext servletContext) {
WebApplicationContext appContext = (WebApplicationContext) servletContext
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Map beans = appContext.getBeansOfType(beanClass);
if (beans == null || beans.isEmpty()) {
return null;
}
return (T) beans.values().toArray()[0];
}
一次WebService调试的遗留问题
在上一篇《一次WebService调试》里,简单的完成了WebService的开发与调用,但还存在着两个问题:
1.客户端生成的代码里,RemoteUser这个对象,每个属性并不是预想中的String,而是javax.xml.bind.JAXBElement<String>。
这个问题可以使用aegis来处理,在RemoteUser所在的包下添加一个RemoteUser.aegis.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:tns="http://www.xxx.com/services/nopService”>
<mapping name="tns:RemoteUser">
<property minOccurs="1" maxOccurs="1" nillable="true" name="fax" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="mail" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="mobile" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="name" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="post" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="pwd" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="sex" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="telephone" />
<property minOccurs="1" maxOccurs="1" nillable="true" name="uid" />
</mapping>
</mappings>
2.客户端生成的代码里,getAccount()的返回值GetAccountResponse里的并不是预想中的List<RemoteUser>,而是一个ArrayOfRemoteUser对象中再包了一个类型为List<RemoteUser>的属性。
现在这个问题还没有解决。。。。。。
一次WebService调试
项目背景:中移动某省的一个管理系统A,而本公司另一产品线则负责此移动公司的门户P,现在需要进行用户数据的同步及实现单点登陆。门户P有一套简单的关于用户数据同步的WebService接口规范,定义了方法名、返回值及参数。
系统A技术框架:JDK1.4 + Struts1.2.x + Spring + XFire。
我对XFire不太熟悉,幸好系统A原先就已经使用了XFire向其他系统提供了WebService,我只需要依葫芦画瓢就行。还是简单的说一下XFire及与Spring集成的配置吧。
1. web.xml的配置,配置xfire的servlet及URI映射
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
2. services.xml的配置,位于WEB-INF/META-INF/xfire下,指定WebService的名称、路径及类接口及实现类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>NopService</name>
<namespace>/services/NopService</namespace>
<serviceClass>com.xxx.project.service.NopService</serviceClass>
<implementationClass>com.xxx.project.service.NopServiceImpl</implementationClass>
</service>
</beans>
服务接口类如下:
package com.xxx.project.service
public interface NopService {
/**
* 获得用户信息
* @param flag
* @return
*/
public List getAccount(String flag);}
getAccount返回的List包含的是对象是RemoteUser,一个纯粹的JavaBean,如下:
package com.xxx.project.service
public class RemoteUser {
private String uid;
private String name;
private String pwd;
private String sex;
private String mobile;
private String telephone;
private String mail;
private String post;
private String fax;/** 省略掉的getter与setter */
}
3. spring beans 的配置
<?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" />
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/nopService">
<ref bean="xfire.nopService"/>
</entry>
</map>
</property>
</bean>
<!– common xfire exporter,set the parent to this bean –>
<bean id="commonXFireExporter"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
lazy-init="false" abstract="true">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
</bean>
<!– Declare a parent bean with all properties common to both services –>
<bean id="xfire.nopService" parent="commonXFireExporter">
<property name="name" value="nopService"/>
<property name="namespace" value="http://www.xxx.com/services/nopService"/>
<property name="serviceBean" ref="nopService" />
<property name="serviceInterface"
value="com.xxx.project.service.NopService" />
</bean>
</beans>
4.aegis的配置,由于接口上的返回类型是集合类,需要配置一个NopService.aegis.xml的文件来告诉xfire包含在集合中的是什么类型,注意到文件名是接口名+.aegis.xml,位置跟接口的一样。
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="getAccount">
<return-type
componentType="com.xxx.project.service.RemoteUser"
minOccurs="1" maxOccurs="1" />
</method>
</mapping>
</mappings>
部署到Tomcat中,通过浏览器访问http://localhost:8080/services/nopService?wsdl,得到了类似如下的WSDL:
<?xml version="1.0
" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://www.xxx.com/services/nopService"
xmlns:tns="http://www.xxx.com/services/nopService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://project.xxx.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://www.xxx.com/services/nopService">
<xsd:element name="getAccount">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0"
nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getAccountResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out"
nillable="true" type="ns1:ArrayOfRemoteUser" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://project.xxx.com">
<xsd:complexType name="ArrayOfRemoteUser">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0"
name="RemoteUser" nillable="true" type="ns1:RemoteUser" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RemoteUser">
<xsd:sequence>
<xsd:element minOccurs="0" name="fax" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="mail" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="mobile" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="name" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="post" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="pwd" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="sex" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="telephone" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="uid" nillable="true"
type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types><!– 省略了部分 –>
</wsdl:definitions>
wsdl文件都出来,算是成功一半了,接着就是生成客户端来测试了。像Axis和cfx一样,XFire也带有代码生成工具,也有ant任务,如下:
<project name="xfire-client" default="wsgen" basedir=".">
<path id="classpath">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
<target name="wsgen">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath" />
<wsgen outputDirectory="appClientModule" wsdl="http://localhost:8080/services/nopService?wsdl" package="com.xxx.project.client" overwrite="true" />
</target>
</project>
客户代码生成后,再写一个测试类来调用WebService:
public class NopServiceTest {
public static void main(String[] args) {
nopServiceClient client = new nopServiceClient();
nopServicePortType service = client.getnopServiceHttpPort("http://localhost:8080/services/nopService");
ArrayOfRemoteUser rets = service.getAccount("");
List<RemoteUser> list = rets.getRemoteUser();
System.out.println(list.size());
}}
OK!测试成功。赶紧打包部署到测试服务器上,让门户P的开发人员来测试。对方很快来了反应:wsdl不符合他们的规范,在将wsdl导入门户P时报错—-无法识别类型“ns1:ArrayOfRemoteUser”。我能得到的错误信息就这么多,以及一份符合所谓规范的wsdl样例,貌似是由Axis生成的:
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions targetNamespace="http://security.interfaces.eoms.yyy.com"
xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://security.interfaces.eoms.yyy.com"
xmlns:intf="http://security.interfaces.eoms.yyy.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!–
WSDL created by Apache Axis version: 1.3 Built on Oct 05, 2005
(05:23:37 EDT)
–>
<wsdl:types>
<schema elementFormDefault="qualified"
targetNamespace="http://security.interfaces.eoms.yyy.com" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="getAccount">
<complexType>
<sequence>
<element name="flag" type="xsd:string" />
</sequence>
</complexType>
</element>
<element name="getAccountResponse">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="getAccountReturn"
type="impl:RemoteUser" />
</sequence>
</complexType>
</element>
<complexType name="RemoteUser">
<seque
nce>
<element name="fax" nillable="true" type="xsd:string" />
<element name="mail" nillable="true" type="xsd:string" />
<element name="mobile" nillable="true" type="xsd:string" />
<element name="name" nillable="true" type="xsd:string" />
<element name="post" nillable="true" type="xsd:string" />
<element name="pwd" nillable="true" type="xsd:string" />
<element name="sex" nillable="true" type="xsd:string" />
<element name="telephone" nillable="true" type="xsd:string" />
<element name="uid" nillable="true" type="xsd:string" />
</sequence>
</complexType>
</schema>
</wsdl:types>
由于非技术原因,此问题得由我来处理。通过对比两个wsdl文件、长时间及多次的测试,最终锁定了门户P无法识别”ns1:ArrayOfRemoteUser”的原因:系统A的wsdl有两个<xsd:schema>节点。又经过多次尝试,才终于把xfire生成的wsdl的schema节点合并为一个,方法是,把前面第3步的spring beans配置中的标红处修改如下,跟接口类颠倒的包名保持一致:
<bean id="xfire.nopService" parent="commonXFireExporter">
<property name="name" value="nopService"/>
<property name="namespace" value="http://service.project.xxx.com"/>
<property name="serviceBean" ref="nopService" />
<property name="serviceInterface"
value="com.xxx.project.service.NopService" />
</bean>
后来在网上找到可能的原因:https://issues.apache.org/jira/browse/CXF-1117,门户P的cxf包可能较旧,不支持多个xmlns。
Struts2中OGNL设置泛型属性值的问题
有这么一个封装了CRUD操作的Action基类:
public abstract class CrudAction extends ActionSupport{
protected T entity;
protected PK id;
public void setId(PK id) {
this.id = id;
}/** 显示新建或编辑页前的数据准备 */
public String input() throws Exception {
}
/** other code */
}
然后是一个继承此CrudAction的用户CRUD操作类:
public class UserAction extends CrudAction {
@Autowired
private UserManager userManager;
@Override
public String input() throws Exception {entity = userManager.get(id);
}
}
但是通过类似这样的连接http://localhost:8080/test/user!input.action?id=uid来访问时,在UserAction中得到的id值是一个长度为1的String数组,导致后面Hibernate查询报错了。不知道为什么获得的值是数组,一开始只好以最直接的办法来获取正确的id值:判断id是否为数组,如果是即取索引为0的值。
后来,尝试在子类用实际的类型覆盖id的setter方法:
@Override
public void setId(String id) {
this.id = id;
}
结果是可以正确的获得值,而不需要经过上面那丑陋的转换。
删除 NetBeans 自动生成的Swing 事件处理方法代码
初用NetBeans6.5开发Swing界面,发现通过NetBeans生成的事件处理方法的代码,在源代码视图里无法删除。Google一下,在http://space.itpub.net/9844649/viewspace-580172找到了正确办法:
选中相关的组件
右键–>“属性”,打开属性对话框,或者直接找到属性视图
点击“事件”页
找到相应的事件,点后面的按钮
在弹出的处理程序对话框中找到相应的事件处理方法,选中删除
Eclipse使用久了,感觉NetBeans相当之不习惯。
SpringSide的网站怎么又访问不了了?
SpringSide还在1.x的时候,有个同事就比较推崇它,而后就断断续续的关注一下,不过也只是远观,并没有花什么精力研究。前一年多的时间里,工作主要是在Eclipse RCP上,也就没有再逛SpringSide。最近换了工作,重拾Java Web开发,于是又开始研究SpringSide。此时,SpringSide3.0都已经发布了。真是应该感谢SpringSide给我们提供的集成那么多Java主流框架的例子与经验。
一边看SpringSide的代码,一边在考虑能否把SpringSide应用到项目当中,便时不时在网上查看SpringSide的Wiki,可前几天却发现,SpringSide网站却访问不了了,到现在还没有恢复。SpringSide创始人江南白衣在JavaEye的博客里,最后发表的文章已经是4月初的了;JavaEye论坛里也没见这次SpringSide网站问题的讨论,却我却看到了去年3月份SpringSide网站也曾经访问不了。希望SpringSide能像去年一样,重新站起来啊,要不然,万一在应用SpringSide的过程中遇到问题,还不好解决呢。期望SpringSide回归!
PS. 昨晚发现满江红开源组织的网站(http://www.redsaga.com)也访问不了,才记起SpringSide的网站空间是由满江红提供的。同样感谢满江红为我们翻译那么多高质量的文档,也同样的期望满江红早日回归!