全局播放框架集响应 header
playframwork set response header globally
如何为我的 playframework 项目全局设置响应 headers?
我不想为每个 REST 方法添加内容。
response().setHeader("Access-Control-Allow-Origin", "*");
response().setHeader("Allow", "*");
response().setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
response().setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
这是我目前添加到每个方法中的内容。
谢谢
Play 支持 filters,这是一种全局修改 HTTP requests/responses 的方式。
类似于:
public class GlobalHeaders extends Filter {
@Inject
public GlobalHeaders(Materializer mat) {
super(mat);
}
@Override
public CompletionStage<Result> apply(
Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
Http.RequestHeader requestHeader) {
return nextFilter.apply(requestHeader).thenApply(result -> {
return result.setHeader("Access-Control-Allow-Origin", "*")
.setHeader("Allow", "*")
.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
});
}
}
2.8 的另一种方式是这样的:https://www.playframework.com/documentation/2.8.x/JavaActionsComposition
你实现了一个注解:
@With(VerboseAnnotationAction.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerboseAnnotation {
boolean someValue() default true;
}
和一个动作:
public class VerboseAnnotationAction extends Action<VerboseAnnotation> {
public CompletionStage<Result> call(Http.Request req) {
// This is you're taking value from the annotation
boolean parameterValue = configuration.someValue();
// decorate the response after calling the controller
return delegate.call(req)
.thenApply(r -> r.withHeader("SomeHeaderName", parameterValue));
}
}
那么你只需将注释与方法或整个控制器一起使用 class:
@VerboseAnnotation(false)
public Result verboseAnnotationIndex() {
return ok("It works!");
}
如何为我的 playframework 项目全局设置响应 headers? 我不想为每个 REST 方法添加内容。
response().setHeader("Access-Control-Allow-Origin", "*");
response().setHeader("Allow", "*");
response().setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
response().setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
这是我目前添加到每个方法中的内容。
谢谢
Play 支持 filters,这是一种全局修改 HTTP requests/responses 的方式。
类似于:
public class GlobalHeaders extends Filter {
@Inject
public GlobalHeaders(Materializer mat) {
super(mat);
}
@Override
public CompletionStage<Result> apply(
Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
Http.RequestHeader requestHeader) {
return nextFilter.apply(requestHeader).thenApply(result -> {
return result.setHeader("Access-Control-Allow-Origin", "*")
.setHeader("Allow", "*")
.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
});
}
}
2.8 的另一种方式是这样的:https://www.playframework.com/documentation/2.8.x/JavaActionsComposition
你实现了一个注解:
@With(VerboseAnnotationAction.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerboseAnnotation {
boolean someValue() default true;
}
和一个动作:
public class VerboseAnnotationAction extends Action<VerboseAnnotation> {
public CompletionStage<Result> call(Http.Request req) {
// This is you're taking value from the annotation
boolean parameterValue = configuration.someValue();
// decorate the response after calling the controller
return delegate.call(req)
.thenApply(r -> r.withHeader("SomeHeaderName", parameterValue));
}
}
那么你只需将注释与方法或整个控制器一起使用 class:
@VerboseAnnotation(false)
public Result verboseAnnotationIndex() {
return ok("It works!");
}