在做公司项目时,需要使用Hessian进行远程调用,网上的方案都是SpringMVC如何整合Hessian,这时候遇到了一个Struts的情况下如何整合Hessian的问题。

在配置Struts时已经进行了如下配置:

    <!-- 1.配置spring配置文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 2.Struts核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 3.Spring核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--使用说明 拦截url为-->
    <servlet>
        <servlet-name>serviceHessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定加载hessian服务端的文件 默认加载 servlet名称-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-hessian-server.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--拦截所有的remoting请求到hessianServer-->
    <servlet-mapping>
        <servlet-name>serviceHessian</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>
    <!-- end hessianServer-->

按照网上找到的一堆各种各样的配置方法,会出现

HessianProtocolException: '' is an unknown code

网上搜索异常解决的方案都是启用hessian客户端的重载

<property name="overloadEnabled" value="true" />

或者是

HessianProxyFactory factory = new HessianProxyFactory(); 
factory.setOverloadEnabled(true); 

其实最终的原因是因为,在web.xml中struts2对所有的请求都进行了拦截,在进行数据转发操作的时候会转发错误导致出现

HessianProtocolException: '' is an unknown code

目前想到的解决是对struts2的过滤器进行更改

<!-- Spring 配置文件 -->
<!-- 1.配置spring配置文件位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-application-content.xml</param-value>
</context-param>
<!-- 2.Struts核心过滤器 -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--拦截所有api开头的请求至Struts2-->
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>
<!-- 3.Spring核心监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
</listener>
<!--4.hessianServer-->
<!--使用说明 拦截url为-->
<servlet>
    <servlet-name>serviceHessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--指定加载hessian服务端的文件 默认加载 servlet名称-servlet.xml-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-hessian-server.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所有的remoting请求到hessianServer-->
<servlet-mapping>
    <servlet-name>serviceHessian</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<!-- end hessianServer-->

最终就不会因为过滤器的原因导致出现异常,但不是完美的解决方法,如果有更好的方法,欢迎留言说明

最后修改:2019 年 03 月 18 日
如果觉得我的文章对你有用,请随意赞赏