cxf webservice解决ip获取不正确的问题

前言:

      CXF 结合spring 开发挺坑爹的,弄个webService麻烦死哥哥我了。首先是开发时,网上的资料与例子抄来抄去,麻烦不说还调不通。其次IP不适用具体IP使用localhost或者127.0.0.1还访问不了。最后,开发完程序部署到was上还出现不兼容的问题。

解决方案:

      一、例子、经过两天的百度查找资料,最终还是自己根据API自己来写了一个,还是蛮简单的哦共大家分享:http://see-you-again.iteye.com/admin/blogs/2296181

      二、解决IP对外开放的问题

             1.spring 配置重新扫描

public class EscapePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	
	private static Logger logger = LoggerFactory.getLogger(EscapePropertyPlaceholderConfigurer.class) ;
    private Set<String> encryptedPropertyNames;

    protected String convertProperty(String propertyName, String propertyValue) {
        if (encryptedPropertyNames.contains(propertyName)) {
            try {
            	logger.info("属性名:"+propertyName+" 原propertyValue:"+propertyValue) ;
                System.out.println(propertyValue);
                if(propertyName.equals("sign.ws.ip")){
                	propertyValue = InetAddress.getLocalHost().getHostAddress() ;//IP替换成本地IP
                }else{
                	propertyValue = Crypt.decode(propertyValue) ;//密码解密
                }
                logger.info("属性名:"+propertyName+" 转换后propertyValue:"+propertyValue) ;
                } catch (Exception e) {
                throw new RuntimeException("属性名:"+propertyName+"转换失败"+e.getMessage());
                }
        }
        return super.convertProperty(propertyName, propertyValue);
    }

    public void setEncryptedPropertyNames(Set<String> encryptedPropertyNames) {
        this.encryptedPropertyNames = encryptedPropertyNames;
    }
}

              二.spring 加载配置

<!-- 加载sit环境配置文件 -->
	<beans profile="sit">
		<bean class="com.op.sign.util.EscapePropertyPlaceholderConfigurer">
			<!-- 加载配置文件列表 -->
			<property name="locations">
				<list>
					<value>classpath:/application-sit.properties</value>
				</list>
			</property>
			<!-- 过滤需要解密的参数 -->
			<property name="encryptedPropertyNames">
				<set>
					<value>sign.ws.ip</value>
				</set>
			</property>
			<property name="fileEncoding" value="UTF-8" />
			<property name="ignoreUnresolvablePlaceholders" value="true" />
		</bean>
	</beans>
	

 

      三、解决cxf 与was冲突的问题

            1.首先冲突的jar包是neethi.jar、xmlschema-core.jar两个jar包。

            2.在was里面创建共享库,将响应版本的jar包放在里面,然后在集群和应用程序中引用共享库。

附录一:创建共享库步骤(样例)

<!--[if !supportLists]-->1.       <!--[endif]-->拷贝以下jar包至${WAS_INSTALL_ROOT}/optionalLibraries/目录下

hibernate-jpa-2.1-api-1.0.0.Final.jar

neethi-3.0.3.jar

xmlschema-core-2.1.0.jar

<!--[if !supportLists]-->2.       <!--[endif]-->设置共享库:

点击环境-->共享库-->选择作用域=tranJQ -->新建

设置名称为:hibernate-jpa-2.1-api-1.0.0.Final,类路径为:${WAS_INSTALL_ROOT}/optionalLibraries/hibernate-jpa-2.1-api-1.0.0.Final.jar-->应用-->保存-->确定

 

点击环境-->共享库-->选择作用域=tranJQ -->新建

设置名称为:neethi-3.0.3,类路径为:${WAS_INSTALL_ROOT}/optionalLibraries/ neethi-3.0.3.jar-->应用-->保存-->确定

 

点击环境-->共享库-->选择作用域=tranJQ -->新建

设置名称为:xmlschema-core-2.1.0,类路径为:${WAS_INSTALL_ROOT}/optionalLibraries/ xmlschema-core-2.1.0.jar-->应用-->保存-->确定

 

<!--[if !supportLists]-->3.       <!--[endif]-->应用共享库:

点击服务器-->集群-->WebSphere Application Server 集群--> tranJQ -->集群成员--> tranNode01-->Java 和进程管理-->类装入器-->新建-->选择:类已装入并且是先使用本地类装入器(父类最后)-->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:hibernate-jpa-2.1-api-1.0.0.Final-->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:neethi-3.0.3 -->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:xmlschema-core-2.1.0 -->应用-->保存-->确定

 


点击服务器-->集群-->WebSphere Application Server 集群--> tranJQ -->集群成员--> tranNode02-->Java 和进程管理-->类装入器-->新建-->选择:类已装入并且是先使用本地类装入器(父类最后)-->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:hibernate-jpa-2.1-api-1.0.0.Final-->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:neethi-3.0.3 -->应用-->保存-->确定

点击刚刚创建的Classloader-->共享库引用-->添加-->选择:xmlschema-core-2.1.0 -->应用-->保存-->确定

猜你喜欢

转载自see-you-again.iteye.com/blog/2298926