简体   繁体   English

如何在JBoss AS上使用Java Restful Web服务启用CORS

[英]How to enable CORS with a java restful web service on JBoss AS

I have a very simple Java RESTful web service that includes two methods for GET and POST requests. 我有一个非常简单的Java RESTful Web服务,其中包括用于GET和POST请求的两种方法。 I am using Jboss Application server 7 on OpenShift platform. 我在OpenShift平台上使用Jboss应用服务器7。

I need to enable JavaScript clients to consume the service using Ajax by sending or receiving JSON data. 我需要通过发送或接收JSON数据来使JavaScript客户端能够使用Ajax使用服务。

The web service is running without problems. Web服务运行正常。 I can consume it using Ajax from a script in the same application, but I need to consume it from other applications on other domains. 我可以从同一应用程序中的脚本使用Ajax来使用它,但是我需要从其他域上的其他应用程序中使用它。

How do I enable Cross-Origin-Request-Sharing (CORS) on my REST endpoints? 如何在我的REST端点上启用跨域请求共享(CORS)?

Add this header to your response: 将此标头添加到您的响应中:

Access-Control-Allow-Origin: *

For example, for servlets you would do this: 例如,对于servlet,您可以这样做:

Alternatively, you can add the jetty-servlets.jar library as a dependency and add this filter to your web.xml. 另外,您可以将jetty-servlets.jar库添加为依赖项,然后将此过滤器添加到web.xml中。 This will allow CORS for all of your web services. 这将允许您的所有Web服务都使用CORS。

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you only need a specific service annotated, first we'll need more information about what library you're using to implement your RESTful service. 如果您仅需要注释的特定服务,则首先我们需要有关用于实现RESTful服务的库的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM