当前位置: 首页 > Java > 正文

struts2 https ajax案例

1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

最近公司搞http 升级为https。在订单取消的时候,一直报以下错误:

2015-09-02 16:06:27,228 ERROR - com.**.web.interceptor.ToolBarInterceptor[72] - JSONUtil.deserialize
org.apache.struts2.json.JSONException: Input string is not well formed JSON (invalid char
at org.apache.struts2.json.JSONReader.buildInvalidInputException(JSONReader.java:155)
at org.apache.struts2.json.JSONReader.read(JSONReader.java:119)
at org.apache.struts2.json.JSONReader.read(JSONReader.java:74)
at org.apache.struts2.json.JSONUtil.deserialize(JSONUtil.java:163)
at org.apache.struts2.json.JSONUtil.deserialize(JSONUtil.java:189)
at com.**.web.interceptor.ToolBarInterceptor.intercept(ToolBarInterceptor.java:70)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.jd.common.struts.interceptor.DotnetSecurityInterceptor.intercept(DotnetSecurityInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(Struts
ActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at com.**.web.action.filter.StrutsCXFFilter.doFilter(StrutsCXFFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(Standar
dEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

根据报错的日志看,前端ajax提交过来的数据,先走JSONInterceptor拦截器,把request的reader序列化,结果导致报错。

Object obj = JSONUtil.deserialize(request.getReader());

当时怀疑前端换用js新版本所致,还原旧版本问题依旧。
在stackoverflow看到,一个https连接 访问http连接js时,jquery会不信任http连接。改为https连接,问题依旧。原文地址:http://stackoverflow.com/questions/14133484/jsonp-request-fails-when-https-is-used-instead-of-http

把$ajax 改为$.getJSON问题依旧。参考文章:http://stackoverflow.com/questions/3780702/jsonp-to-acquire-json-from-https-protocol-with-jquery

经过检查源码,去掉JSONInterceptor并未对原来系统有影响,故决定struts配置去掉其拦截配置。

<package name="toolbar-struts" extends="struts-default">
		<result-types>
			<result-type name="json" class="org.apache.struts2.json.JSONResult" />
		</result-types>
		<interceptors>
			<!--<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />-->
			<interceptor name="methodInterceptor" class="toolBarInterceptor">
				<param name="excludeMethods">showListCancelOrderButton</param>
				<param name="includeMethods"></param>
			</interceptor>
 
 
			<interceptor-stack name="toolbarInterceptorStack">
				<!--<interceptor-ref name="json" />-->
				<interceptor-ref name="exception">
					<param name="logEnabled">true</param>
					<param name="logLevel">ERROR</param>
				</interceptor-ref>
				<interceptor-ref name="alias" />
				<interceptor-ref name="servletConfig" />
				<interceptor-ref name="i18n" />
				<interceptor-ref name="prepare" />
				<interceptor-ref name="chain" />
				<interceptor-ref name="debugging" />
				<interceptor-ref name="scopedModelDriven" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="toolbarInterceptorStack" />
	</package>

本文固定链接: http://www.chepoo.com/struts2-https-ajax-case.html | IT技术精华网

struts2 https ajax案例:等您坐沙发呢!

发表评论