spark-微型的Java Web框架 Spark Framework

前端之家收集整理的这篇文章主要介绍了spark-微型的Java Web框架 Spark Framework前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Spark是一个微型的Java Web框架,它的灵感来自于Sinatra,它的目的是让你以最小的代价创建出一个Java Web应用。

Implement CORS in Spark -spark中如何处理跨域资源共享问题
代码如下:

// Enables CORS on requests. This method is an initialization method and should be called once.
private static void enableCORS(final String origin,final String methods,final String headers) {

options("/*",(request,response) -> {

   String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
   if (accessControlRequestHeaders != null) {
       response.header("Access-Control-Allow-Headers",accessControlRequestHeaders);
   }

   String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
   if (accessControlRequestMethod != null) {
       response.header("Access-Control-Allow-Methods",accessControlRequestMethod);
   }

   return "OK";

});

before((request,response) -> {
response.header("Access-Control-Allow-Origin",origin);
response.header("Access-Control-Request-Method",methods);
response.header("Access-Control-Allow-Headers",headers);
// Note: this may or may not be necessary in your particular application
response.type("application/json");
});
}

说明: before

 Before-filters are evaluated before each request,and can read the request and read/modify the response.

除此之外还有一个after

 After-filters are evaluated after each request,and can read the request and read/modify the response:

猜你在找的程序笔记相关文章