Interceptor Play框架2.3.8如何修改response Result Body?
How to Modify response Result Body in Interceptor Play framework 2.3.8?
@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;
});
}
我想修改拦截器的响应主体?。我可以得到结果体。以便我修改响应。如何覆盖现有的主体响应和结果作为结果。
正文内容是不是这样更新的
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;
});
@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;
});
}
我想修改拦截器的响应主体?。我可以得到结果体。以便我修改响应。如何覆盖现有的主体响应和结果作为结果。
正文内容是不是这样更新的
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;
});