公司主项目之前使用的spring-integration版本为2.2.x,本来计划转到springboot上,但考虑到现在代码体系结构比较复杂,时间比较紧迫,故先升级到4.3.x。
版本的选择
原则就是直接使用springboot最新版本中依赖的组件版本号,参考的springboot版本为1.5.16(因为生产环境jdk等原因不计划直接使用springboot2),对应的spring-integration版本为4.3.17.RELEASE。
1.路径参数获取报错
其中一个模块使用到了spring-integration-http,所以也进行了升级。但调试时发现原接口无法调用成功,报错模块spring-expression,EL1007E,使用了pathVariables,配置如下:
<int-http:inbound-gateway id="inboundGateway" path="/{name}/{id}" ... >
<int-http:header name="name" expression="#pathVariables.name"/>
<int-http:header name="id" expression="#pathVariables.id"/>
</int-http:inbound-gateway>
经过详细排查,发现是ling438处所得的pathVariables为空;
对比旧版本的代码:
发现pathVariables的获取方式发生了变化(红色框内代码):
旧版本直接匹配获取,新版本是从request的attribuate中获取的。
这时需要查找何时将这个attribuate放到request中的。
先要有个成功的新版本工程,此处参考了github上的spring-integration的样例,主要是rest-http的部分,谢谢作者:
https://github.com/spring-projects/spring-integration-samples
其实一开始就发现了端倪:公司项目的web.xml中的servlet配置为
而样例项目中的配置为
**没有用DispatcherServlet!**所用从DispatcherServlet开始debug(过程不累述),终于找到了何处处理了request的attribuate…下面就是如何修改了!大的方式未改,写了一个新的Servlet类继承HttpRequestHandlerServlet,改写方法,并配置在web.xml中:
这样就能成功获取到路径中的参数了!
2.参数取值不正确问题
虽然取参数不报错了,但是取出来的参数不正确,原因是路径中有.(英文“点”),最后一个.之后的字符串都被截掉了!
修改后:
<int-http:inbound-gateway id="inboundGateway" path="/{name}/{id:[a-zA-Z0-9\.]+}" ... >
<int-http:header name="name" expression="#pathVariables.name"/>
<int-http:header name="id" expression="#pathVariables.id"/>
</int-http:inbound-gateway>
总结:
不是特殊原因,请严格参考官方样例配置…