Web services Restlet&x2B;JAXRS扩展-如何使用过滤器?

Web services Restlet&x2B;JAXRS扩展-如何使用过滤器?,web-services,rest,jersey,cors,restlet,Web Services,Rest,Jersey,Cors,Restlet,我在Restlet+JAXRS扩展中实现了一个REST服务。 在某个时刻,我不得不将CORS标题添加到响应中。 我有很多REST调用,并在工作时手动添加标题: return Response.status(200).header("Access-Control-Allow-Origin", "*"). header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested

我在Restlet+JAXRS扩展中实现了一个REST服务。 在某个时刻,我不得不将CORS标题添加到响应中。 我有很多REST调用,并在工作时手动添加标题:

        return Response.status(200).header("Access-Control-Allow-Origin", "*").
                header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type").
                header("Access-Control-Expose-Headers", "Location, Content-Disposition").
                header("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, HEAD, OPTIONS").
                entity(fsJSON).build();
但我希望使用过滤器将这些标题添加到所有响应中,而无需手动添加这些标题。我发现了很多在JAX-RS中使用过滤器的例子,比如:

但我不明白如何将它们与Restlet+JAX-RS环境集成。例如,我在任何地方都看不到ContainerResponseFilter类。
任何人都可以帮助我吗?

在Restlet中创建JaxRS应用程序时,您可以创建一个
JaxRS应用程序(请参见以下链接:)。此类扩展了Restlet的标准应用程序。后者提供了使用
getServices
方法在其上配置服务的方法

所以在你的情况下,你不需要使用过滤器

有关Restlet的CorsService的配置,请参见以下答案:

下面是在Restlet JaxRS应用程序中配置CORS的方法:

Component comp = new Component();
Server server = comp.getServers().add(Protocol.HTTP, 8182);

JaxRsApplication application = new JaxRsApplication(comp.getContext());
application.add(new ExampleApplication());

CorsService corsService = new CorsService();         
corsService.setAllowedOrigins(new HashSet(Arrays.asList("*")));
corsService.setAllowedCredentials(true);

application.getServices().add(corsService);
component.getDefaultHost().attachDefault(application);
否则,Restlet的相应扩展不支持JAX-RS过滤器。要添加筛选器,需要将其作为Restlet筛选器添加到应用程序前面,如下所述:

JaxRsApplication application = new JaxRsApplication(comp.getContext());
application.add(new ExampleApplication());

MyRestletFilter filter = new MyRestletFilter();
filter.setNext(application);

component.getDefaultHost().attachDefault(filter);
希望它能帮助你,
Thierry

在Restlet中创建JaxRS应用程序时,需要创建一个
JaxRS应用程序(请参见以下链接:)。此类扩展了Restlet的标准应用程序。后者提供了使用
getServices
方法在其上配置服务的方法

所以在你的情况下,你不需要使用过滤器

有关Restlet的CorsService的配置,请参见以下答案:

下面是在Restlet JaxRS应用程序中配置CORS的方法:

Component comp = new Component();
Server server = comp.getServers().add(Protocol.HTTP, 8182);

JaxRsApplication application = new JaxRsApplication(comp.getContext());
application.add(new ExampleApplication());

CorsService corsService = new CorsService();         
corsService.setAllowedOrigins(new HashSet(Arrays.asList("*")));
corsService.setAllowedCredentials(true);

application.getServices().add(corsService);
component.getDefaultHost().attachDefault(application);
否则,Restlet的相应扩展不支持JAX-RS过滤器。要添加筛选器,需要将其作为Restlet筛选器添加到应用程序前面,如下所述:

JaxRsApplication application = new JaxRsApplication(comp.getContext());
application.add(new ExampleApplication());

MyRestletFilter filter = new MyRestletFilter();
filter.setNext(application);

component.getDefaultHost().attachDefault(filter);
希望它能帮助你,
Thierry

我添加了关于JAX-RS扩展的过滤器的一般说明…我添加了关于JAX-RS扩展的过滤器的一般说明。。。