Spring WS Client在回调之外更新变量

时间:2014-07-25 16:18:22

标签: java spring soap callback spring-ws

我正在从WebServiceMessageCallback中提取HTTP响应,但是当我尝试更新回调之外的变量时,我得到:不能引用在另一个方法中定义的内部类中的非final变量rawResponse“

String rawResponse=null;
try { 
    this.webserviceTemplate.sendSourceAndReceiveToResult(source,
        new WebServiceMessageCallback() {
                @Override
                public void doWithMessage(WebServiceMessage message)
                        throws IOException, TransformerException {
                    TransportContext context = TransportContextHolder.getTransportContext();
                    CommonsHttpConnection  connection = (CommonsHttpConnection  )context.getConnection();
                    context.getConnection();
                    rawResponse = connection.getPostMethod().getResponseBodyAsString();*/

1 个答案:

答案 0 :(得分:0)

你真的应该使用final变量来做。 AtomicReference适合您:

final AtimicReference<String> rawResponse = new AtimicReference<String>();
try { 
    this.webserviceTemplate.sendSourceAndReceiveToResult(source,
        new WebServiceMessageCallback() {
                @Override
                public void doWithMessage(WebServiceMessage message)
                        throws IOException, TransformerException {
...
                    rawResponse.set(connection.getPostMethod().getResponseBodyAsString());