无法在Play with WSClient中从HTTP服务器中取消重新定义WSresponse asJson()

我从HTTP服务获得WSResponse(表单Play框架)响应

{“authToken”:“d4b4bf6d5ff3f35366a75b498c8dbb58”}

我试图读作JSON并在控制台上使用打印

System.out.println(response.asJson());

我正确地在控制台上打印了响应,但是,代码在函数asJson()中断了以下跟踪:

play.api.http.HttpErrorHandlerExceptions $$ anon $ 1:执行异常[[RuntimeException:com.fasterxml.jackson.databind.JsonMappingException:由于[Source:org.jboss.netty]的输入结束,没有要映射的内容 . buffer.ChannelBufferInputStream@23408f7c; line:1,column:1]]] at play.api.http.HttpErrorHandlerExceptions $ .throwableToUsefulException(HttpErrorHandler.scala:265)〜[play_2.10-2.4.2.jar:2.4.2] at play.api.http .DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191)〜[play_2.10-2.4.2.jar:2.4.2] at play.api.GlobalSettings $ class.onError(GlobalSettings.scala:179)[play_2.10-2.4 .2.jar:2.4.2] at play.api.DefaultGlobal $ .onError(GlobalSettings.scala:212)[play_2.10-2.4.2.jar:2.4.2] at play.api.http.GlobalSettingsHttpErrorHandler.onServerError (HttpErrorHandler.scala:94)[play_2.10-2.4.2.jar:2.4.2] at play.filters.cors.AbstractCORSPolicy $$ anonfun $ 1.applyOrElse(AbstractCORSPolicy.scala:151)[filters-helpers_2.10- 2.4.2.jar:2.4.2] at scala.filters.cors.AbstractCORSPolicy $$ anonfun $ 1.applyOrElse(AbstractCORSPolicy.scala:150)[filters-helpers_2.10-2.4.2.jar:2.4.2] at scala .concurrent.Future $$ anonfun $ recoverWith $ 1.apply(Future.scala:344)[scala-library-2.10.5.jar:na] at scala.concurrent.Future $$ anonfun $ recoverWith $ 1.apply(Future.scala :343)[scala-library-2.10.5.jar:na] at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)[scala-library-2.10.5.jar:na] at play.api .libs.iteratee.Execution $ trampoline $ .execute(Execution.scala:70)[play-iteratees_2.10-2.4.2.jar:2.4.2] at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala: 40)[scala-library-2.10.5.jar:na] at scala.concurrent.impl.Promise $ DefaultPromise.tryComplete(Promise.scala:248)[scala-library-2.10.5.jar:na] at scala . concurrent.Promise $ class.tryFailure(Promise.scala:112)[scala-library-2.10.5.jar:na] at scala.concurrent.impl.Promise $ DefaultPromise.tryFailure(Promise.scala:153)[scala-library -2.10.5.jar:na] at play.api.mvc.Filter $$ anon $ 1 $$ anonfun $ apply $ 4 $$ anonfun $ apply $ 1.applyOrElse(Filters.scala:67)[play_2.10-2.4.2 .jar:2.4.2] at play.api.mvc.Filter $$ anon $ 1 $$ anonfun $ apply $ 4 $$ anonfun $ apply $ 1.applyOrElse(Filters.scala:61)[play_2.10-2.4.2.jar :2.4.2]在scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)[scala-libr ary-2.10.5.jar:na] at play.api.libs.iteratee.Iteratee $$ anonfun $ recoverM $ 1.applyOrElse(Iteratee.scala:629)[play-iteratees_2.10-2.4.2.jar:2.4 . 2] at play.api.libs.iteratee.Iteratee $$ anonfun $ recoverM $ 1.applyOrElse(Iteratee.scala:629)[play-iteratees_2.10-2.4.2.jar:2.4.2] at scala.runtime.AbstractPartialFunction .apply(AbstractPartialFunction.scala:33)[scala-library-2.10.5.jar:na]在scala.util.Failure $$ anonfun $ recover $ 1.apply(Try.scala:185)[scala-library-2.10 . 5.jar:na] at scala.util.Try $ .apply(Try.scala:161)[scala-library-2.10.5.jar:na] at scala.util.Failure.recover(Try.scala:185) [scala-library-2.10.5.jar:na] at scala.concurrent.Future $$ anonfun $ recover $ 1.apply(Future.scala:324)[scala-library-2.10.5.jar:na] at scala . concurrent.Future $$ anonfun $ recover $ 1.apply(Future.scala:324)[scala-library-2.10.5.jar:na] at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)[scala -library-2.10.5.jar:na] at akka.dispatch.BatchingExecutor $ AbstractBatch.processBatch(BatchingExecutor.scala:55)[ akka-actor_2.10-2.3.11.jar:na] at akka.dispatch.BatchingExecutor $ BlockableBatch $$ anonfun $ run $ 1.apply $ mcV $ sp(BatchingExecutor.scala:91)[akka-actor_2.10-2.3 . 11.jar:na] at akka.dispatch.BatchingExecutor $ BlockableBatch $$ anonfun $ run $ 1.apply(BatchingExecutor.scala:91)[akka-actor_2.10-2.3.11.jar:na] at akka.dispatch.BatchingExecutor $ blockableBatch $$ anonfun $ run $ 1.apply(BatchingExecutor.scala:91)[akka-actor_2.10-2.3.11.jar:na] at scala.concurrent.BlockContext $ .withBlockContext(BlockContext.scala:72)[scala -library-2.10.5.jar:na] at akka.dispatch.BatchingExecutor $ BlockableBatch.run(BatchingExecutor.scala:90)[akka-actor_2.10-2.3.11.jar:na] at akka.dispatch.TaskInvocation . run(AbstractDispatcher.scala:40)[akka-actor_2.10-2.3.11.jar:na] atakka.dispatch.ForkJoinExecutorConfigurator $ AkkaForkJoinTask.exec(AbstractDispatcher.scala:397)[阿卡-actor_2.10-2.3.11.jar:NA]在scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)阶-library-2.10.5.jar:NA]在scala.concurrent.forkjoin.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool.java:1339)[阶库-2.10.5.jar:NA]在scala.concurrent.forkjoin . ForkJoinPool.runWorker(ForkJoinPool.java:1979)scala.concurrent.forkjoin.ForkJoinWorkerThread.run [scala-library-2.10.5.jar:na] [scala-library-2.10.5.jar]率:na]产生的原因:了java.lang.RuntimeException:com.fasterxml.jackson.databind.JsonMappingException:没有内容映射,由于在[来源输入结束:org.jboss.netty.buffer.ChannelBufferInputStream@23408f7c;在play.libs.ws.ning的play.libs.Json.parse(Json.java:149)〜[play-json_2.10-2.4.2.jar:2.4.2]中行:1,列:1] . NingWSResponse.asJson(NingWSResponse.java:149)〜[播放java的ws_2.10-2.4.2.jar:2.4.2]在service.GlaasService.lambda $ $ getAuthToken 0(GlaasService.java:28)〜[类/ :na] at service.GlaasService $$ Lambda $ 74 / 930311498.apply(Unknown Source)〜[na:na] at play.core.j.FPromiseHelper $$ anonfun $ map $ 1.apply(FPromiseHelper.scala:103)〜[ play_2.10-2.4.2.jar:2.4.2]在scala.util.Success $$ anonfun $ map $ 1.apply(Try.scala:206)〜[scala-library-2.10.5.jar:na] at scala.util.Try $ .apply(Try.scala:161)[scala-library-2.10.5.jar:na]在scala.util.Success.map(Try.scala:206)〜[scala-library-2.10 .5.jar:na] at scala.concurrent.Future $$ anonfun $ map $ 1.apply(Future.scala:235)〜[scala-library-2.10.5.jar:na] at scala.concurrent.Future $$ anonfun $ map $ 1.apply(Future.scala:235)〜[scala-library-2.10.5.jar:na] at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)[scala-library-2.10 .5.jar:na] at play.core.j.Htt pExecutionContext $$ anon $ 2.run(HttpExecutionContext.scala:40)〜[play_2.10-2.4.2.jar:2.4.2] ... 6个常见的帧省略引起:com.fasterxml.jackson.databind.JsonMappingException:由于输入结束而没有要映射的内容[来源:org.jboss.netty.buffer.ChannelBufferInputStream@23408f7c;在com.fasterxml.jackson的com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)〜[jackson-databind-2.5.4.jar:2.5.4]中的第1行,第1列:第1列 . databind.ObjectMapper._initForReading(ObjectMapper.java:3609)〜[ Jackson 数据绑定-2.5.4.jar:2.5.4]在com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3549)〜[ Jackson -databind-2.5.4.jar:2.5.4] at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2078)〜[jackson-databind-2.5.4.jar:2.5.4]正在播放.libs.Json.parse(Json.java:147)〜[play-json_2.10-2.4.2.jar:2.4.2] ...省略了17个常用帧

错误的原因看起来像json的输入不合适,但我验证了JSON响应,如果工作正常,并且控制台语句也正确打印它,但它打印后打破它 . 如果我尝试在没有打印的情况下将响应作为JSON处理,它仍然会以相同的异常中断 .

添加方法代码如下:

public static Promise<Result> getAuthToken(String serviceProfileId, String username, String password, WSClient ws){
                return ws.url("http://SERVER_URL/auth")
                .setContentType("application/x-www-form-urlencoded")
                .post("serviceID="+serviceProfileId+"&username="+username+"&password="+password)
        .map( response -> {
            if(response == null)
                return play.mvc.Results.noContent();
            System.out.println(response.asJson());
            JsonNode json = response.asJson();
            if(json == null) {
                return play.mvc.Results.ok("Expecting Json data");
            } else {
                String token = json.findPath("authToken").textValue();
                if(token == null) {
                  return play.mvc.Results.ok("Missing parameter [auth Token]");
                } else {
                  return play.mvc.Results.ok("Hello " + token);
                }
            }

        });     }

欢迎任何有关如何解决此问题的意见 . 谢谢 .

回答(0)