Java 访问控制允许ajax调用jersey rest web服务中的源代码

Java 访问控制允许ajax调用jersey rest web服务中的源代码,java,ajax,json,rest,cors,Java,Ajax,Json,Rest,Cors,我使用jersey JAX Rs作为web服务来查询mysql。我尝试通过混合移动应用程序使用web服务 我指的是这个 在服务器端,在tomcat server7中运行以下代码以查询sql @Path("/employees") public class EmployeeResource { EmployeeDAO dao = new EmployeeDAO(); @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICA

我使用jersey JAX Rs作为web服务来查询mysql。我尝试通过混合移动应用程序使用web服务

我指的是这个

在服务器端,在tomcat server7中运行以下代码以查询sql

@Path("/employees")
 public class EmployeeResource {

EmployeeDAO dao = new EmployeeDAO();

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Employee> findAll() {
    return dao.findAll();
}
}
实际上,我尝试使用客户机和服务器基础结构开发应用程序。因此,我需要在客户机中使用html文件来使用服务器端的jersey web服务。如果我从另一个项目中的html文件调用$.getJSON或$.ajax来使用web服务,这将非常有用

when i use this url http://localhost:8181/jQueryJAXRS/rest/employees  in my browser
它显示了xml

<employees>
<employee>
<firstName>john</firstName>
<id>1</id>
<lastName>doe</lastName>
<reportCount>0</reportCount>
<title>trainee</title>
</employee>
<employee>
<firstName>james</firstName>
<id>2</id>
<lastName>dane</lastName>
<reportCount>0</reportCount>
<title>developer</title>
</employee>
<employee>
<firstName>ramsy</firstName>
<id>4</id>
<lastName>stuart</lastName>
<reportCount>0</reportCount>
<title>QA</title>
</employee>
</employees>

厕所
1.
雌鹿
0
练习生
詹姆斯
2.
丹麦人
0
开发商
拉姆齐
4.
斯图尔特
0
质量保证
但当我尝试通过脚本的方式,它会显示CORS错误发生。提出一些想法对我很有帮助

提前感谢。

您必须使用

为此:

  • 你必须使用
  • 然后,您必须在web.xml中使用过滤器:

    <filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, HEAD, POST, OPTIONS</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Content-Type, X-Requested-With</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposedHeaders</param-name>
        <param-value>X-Test-1, X-Test-2</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>-1</param-value>
    </init-param>
    </filter>
    
    
    <filter-mapping>
    <filter-name>CORS</filter-name>
     <url-pattern>/EmployeeResource</url-pattern>
    </filter-mapping>
    
    
    科尔斯
    com.thetransactioncompany.cors.CORSFilter
    cors.allowgenerichtt前传
    真的
    金鸡儿
    *
    cors.allowSubdomains
    真的
    cors.supportedMethods
    获取、头、柱、选项
    cors.supportedHeaders
    内容类型,X-request-With
    cors.exposedHeaders
    X-Test-1,X-Test-2
    cors.supportsCredentials
    真的
    科斯马克萨奇酒店
    -1
    科尔斯
    /雇员资源
    

  • 感谢约翰维提出这个CORS想法。在这里,我将进行更多的解释,以使其更清晰,并使其成为该问题的完整解决方案

    我引用此链接来解决此问题

    下载CORS jar和java属性jar。将这些jar放在tomcat服务器的lib文件夹中。然后在web.xml中添加以下过滤器作为web应用程序的chlidnode

    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>CORS</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    科尔斯
    com.thetransactioncompany.cors.CORSFilter
    科尔斯
    /*
    

    此解决方案将解决CORS问题。

    谢谢您的提问!通过使用maven并将依赖项添加到pom.xml中,我找到了一种导入jar的较短方法

    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>java-property-utils</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>1.9.1</version>
    </dependency>
    
    
    交易公司
    

    如果有人不想添加第三方库,他们可以创建自己的过滤器来添加所需的标题作为响应。
    请参阅

    谢谢詹维。我应该在项目的buildpath中添加jar,对吗?然后将这些xml作为webapp节点的子节点添加到web.xml中。如果我走错了路,请纠正我。EmployeeDirectory Jersey com.sun.Jersey.spi.container.servlet.ServletContainer com.sun.Jersey.config.property.packages org.coenraets 1 Jersey/rest/*在添加CORS jar后,您现在的http状态为404
    <employees>
    <employee>
    <firstName>john</firstName>
    <id>1</id>
    <lastName>doe</lastName>
    <reportCount>0</reportCount>
    <title>trainee</title>
    </employee>
    <employee>
    <firstName>james</firstName>
    <id>2</id>
    <lastName>dane</lastName>
    <reportCount>0</reportCount>
    <title>developer</title>
    </employee>
    <employee>
    <firstName>ramsy</firstName>
    <id>4</id>
    <lastName>stuart</lastName>
    <reportCount>0</reportCount>
    <title>QA</title>
    </employee>
    </employees>
    
    <filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, HEAD, POST, OPTIONS</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Content-Type, X-Requested-With</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposedHeaders</param-name>
        <param-value>X-Test-1, X-Test-2</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>-1</param-value>
    </init-param>
    </filter>
    
    
    <filter-mapping>
    <filter-name>CORS</filter-name>
     <url-pattern>/EmployeeResource</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>CORS</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>java-property-utils</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>1.9.1</version>
    </dependency>