java.lang.IllegalStateException: closed

Ebaugh發表於2020-11-11

java.lang.IllegalStateException: closed
    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
    at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
    at okhttp3.ResponseBody.string(ResponseBody.java:174)
    at com.nextop.controller.EBayController.getOrders(EBayController.java:151)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

 

OkHttpClient的返回值呼叫兩次導致流已經關閉  response.body().string();

原因是流已經關閉,所以無法再進行操作。

 

@PostMapping(value = "getOrders", produces = {"application/json"})
    public String getOrders(@RequestBody final String token) throws IOException {
        final OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
//                .url("https://api.sandbox.ebay.com/sell/fulfillment/v1/order/04-05321-94491")
                .url("https://api.sandbox.ebay.com/sell/fulfillment/v1/order?filter=creationdate:%5B2020-11-01T15:05:43.026Z..%5D&limit=50&offset=0")
//                .url("https://api.ebay.com/sell/fulfillment/v1/order/04-05321-94491")
                .get()
                .addHeader("Authorization", "Bearer " + token)
                .addHeader("Content-Language", "EBAY_US")
                .addHeader("Content-Type", "application/json")
                .addHeader("X-EBAY-C-MARKETPLACE-ID", "EBAY_US")
                .build();
        final Response response = okHttpClient.newCall(request).execute();
        log.info("請求是否成功: " + response.isSuccessful());
        log.info("response" + response.body().string());
        return response.body().string();
    }

 

OkHttp請求回撥中response.body().string()只能有效呼叫一次,呼叫response.body().string()的時候資料流已經關閉了,再次呼叫就是提示已經closed。然後我把程式碼修改完之後就可以了,只呼叫一次response.body().string();

@PostMapping(value = "getOrders", produces = {"application/json"})
    public String getOrders(@RequestBody final String token) throws IOException {
        final OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
//                .url("https://api.sandbox.ebay.com/sell/fulfillment/v1/order/04-05321-94491")
                .url("https://api.sandbox.ebay.com/sell/fulfillment/v1/order?filter=creationdate:%5B2020-11-01T15:05:43.026Z..%5D&limit=50&offset=0")
//                .url("https://api.ebay.com/sell/fulfillment/v1/order/04-05321-94491")
                .get()
                .addHeader("Authorization", "Bearer " + token)
                .addHeader("Content-Language", "EBAY_US")
                .addHeader("Content-Type", "application/json")
                .addHeader("X-EBAY-C-MARKETPLACE-ID", "EBAY_US")
                .build();
        final Response response = okHttpClient.newCall(request).execute();
        log.info("請求是否成功: " + response.isSuccessful());
  //      log.info("response" + response.body().string());
        return response.body().string();
    }

 

相關文章