如何从play应用程序中的play.mvc.Result对象提取结果内容? [英] How to extract result content from play.mvc.Result object in play application?

查看:144
本文介绍了如何从play应用程序中的play.mvc.Result对象提取结果内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在从一个播放应用程序重定向到另一个播放应用程序,最后我收到了作为Result对象的响应.以下是两个应用程序中的操作.我正在从apllication1重定向到application2.应用程序2将返回我需要提取的JSON字符串.

actually I am doing redirect from one play application to another play application, finally I receive response as Result object.. Below is the action in two applications. I am redirecting from apllication1 to application2. Application 2 will return JSON string, that I need to extract.

如何从Result对象检索JSON内容?

How can I retrieve JSON content from Result object?

应用程序1:

public static Result redirectTest(){

    Result result =  redirect("http://ipaddress:9000/authenticate");
    /*** here I would like to extract JSON string from result***/
    return result;
}

应用2:

@SecuredAction
public static Result index() {
     Map<String, String> response = new HashMap<String, String>();
     DemoUser user = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);

       for(BasicProfile basicProfile: user.identities){
           response.put("name", basicProfile.firstName().get());
           response.put("emailId", basicProfile.email().get());
           response.put("providerId", basicProfile.providerId());
           response.put("avatarurl", basicProfile.avatarUrl().get());
       }

    return ok(new JSONObject(response).toString());
}

推荐答案

使用JavaResultExtractor,例如:

Use JavaResultExtractor, example:

Result result = ...;
byte[] body = JavaResultExtractor.getBody(result, 0L);

具有字节数组,您可以从Content-Type标头中提取字符集并创建java.lang.String:

Having a byte array, you can extract charset from Content-Type header and create java.lang.String:

String header = JavaResultExtractor.getHeaders(result).get("Content-Type");
String charset = "utf-8";
if(header != null && header.contains("; charset=")){
    charset = header.substring(header.indexOf("; charset=") + 10, header.length()).trim();
}
String bodyStr = new String(body, charset);
JsonNode bodyJson = Json.parse(bodyStr);

上面的一些代码是从播放中复制的.test.Helpers

这篇关于如何从play应用程序中的play.mvc.Result对象提取结果内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆