如何在Interceptor Play框架2.3.8中修改响应结果体?

时间:2018-04-19 10:44:51

标签: interceptor playframework-2.3

 @Override
public Promise<Result> call(Http.Context ctx) throws java.lang.Throwable {
     Promise<Result> result = this.delegate.call(ctx);
     Http.Response response = ctx.response();
     response.setHeader("Access-Control-Allow-Origin", "*");
     return result.map(r -> {
        play.api.mvc.Result res =  r.toScala();
        final int status = r.toScala().header().status();
        if(status == 200 || status == 201) {
            String responseBody = new String(JavaResultExtractor.getBody(r, 0L));
            final Map<String,String> headers =  JavaResultExtractor.getHeaders(r);
            ResponseHeader responseHeader = res.header();
            return new Result(responseHeader, res.body());
        }
        return r;
     });
}

我想修改拦截器的响应体?我能够得到结果体。这样我就会修改响应。如何覆盖现有的身体反应和结果作为结果。

1 个答案:

答案 0 :(得分:0)

身体内容是否像这样更新。

return result.map(r -> {
                final int status = r.toScala().header().status();
                if(status == 200 || status == 201) {
                    // Can change the body content here
                    Status s = status(status,new String(JavaResultExtractor.getBody(r, 0L)));
                    return s.as(JavaResultExtractor.getHeaders(r).get("Content-Type"));
                }
                return r;
             });