#今天遇到一个需求,要以xml格式信息返回调用平台
正确:
<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<STATUS>200</STATUS>
<DESC>请求成功</DESC>
<TIME>20230509172356</TIME>
<DATA> </DATA>
</RESULT>
错误:
“<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<STATUS>200</STATUS>
<DESC>请求成功</DESC>
<TIME>20230509172356</TIME>
<DATA> </DATA>
</RESULT>”
public void receptSblsh(){
//入参报文信息
String xmlStr = ServletActionContext.getRequest().getParameter("xmlStr");
//返回报文信息
String xmlResult = this.getService().receptSblsh(xmlStr);
ServletActionContext.getResponse().setHeader("Content-Type", "application/xml;charset=utf-8");
//添加以下代码能够满足返回xml格式信息,而不是字符串格式xml信息
try {
ServletActionContext.getResponse().getWriter().write(xmlResult);
ServletActionContext.getResponse().getWriter().flush();
ServletActionContext.getResponse().getWriter().close();
} catch (IOException e) {
e.printStackTrace();
}
this.setReturnData(xmlResult);
}
**运行完成后会提示报错,但会提示以下错误,哪位大神看到的话,还请指点一二!
java.lang.IllegalStateException: getWriter() has already been called for this response
at com.tongweb.web.thor.connector.Response.getOutputStream(Response.java:604)
at com.tongweb.web.thor.connector.ResponseFacade.getOutputStream(ResponseFacade.java:197)
at javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:96)
at org.apache.struts2.rest.DefaultContentTypeHandlerManager.handleResult(DefaultContentTypeHandlerManager.java:199)
at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:232)
at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:197)
at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:145)
at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:147)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:575)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at com.tongweb.web.thor.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at com.tongweb.web.thor.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
at com.tongweb.web.thor.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at com.tongweb.web.thor.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at com.tongweb.web.thor.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at com.tongweb.web.thor.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at com.tongweb.web.thor.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at com.tongweb.web.thor.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.tongweb.web.thor.core.ThorStandardWrapperValve.invoke(ThorStandardWrapperValve.java:202)
at com.tongweb.web.thor.core.StandardContextValve.invoke(StandardContextValve.java:240)
at com.tongweb.twnt.thor.OpenEJBValve.invoke(OpenEJBValve.java:46)
at com.tongweb.web.thor.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at com.tongweb.web.thor.core.ThorStandardHostValve.invoke(ThorStandardHostValve.java:69)
at com.tongweb.web.thor.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at com.tongweb.web.thor.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at com.tongweb.web.thor.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.tongweb.web.thor.connector.CoyoteAdapter.service(CoyoteAdapter.java:411)
at com.tongweb.web.oro.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
at com.tongweb.web.oro.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at com.tongweb.web.webutil.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1788)
at com.tongweb.web.webutil.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1765)
at com.tongweb.web.webutil.util.threads.TWThreadPoolExecutor.runWorker(TWThreadPoolExecutor.java:1165)
at com.tongweb.web.webutil.util.threads.TWThreadPoolExecutor$Worker.run(TWThreadPoolExecutor.java:622)
at java.lang.Thread.run(Thread.java:745)
]