• <legend id='RTjtY'><style id='RTjtY'><dir id='RTjtY'><q id='RTjtY'></q></dir></style></legend>
    <tfoot id='RTjtY'></tfoot>

      1. <small id='RTjtY'></small><noframes id='RTjtY'>

        • <bdo id='RTjtY'></bdo><ul id='RTjtY'></ul>

      2. <i id='RTjtY'><tr id='RTjtY'><dt id='RTjtY'><q id='RTjtY'><span id='RTjtY'><b id='RTjtY'><form id='RTjtY'><ins id='RTjtY'></ins><ul id='RTjtY'></ul><sub id='RTjtY'></sub></form><legend id='RTjtY'></legend><bdo id='RTjtY'><pre id='RTjtY'><center id='RTjtY'></center></pre></bdo></b><th id='RTjtY'></th></span></q></dt></tr></i><div id='RTjtY'><tfoot id='RTjtY'></tfoot><dl id='RTjtY'><fieldset id='RTjtY'></fieldset></dl></div>

        RESTful webservice:如何在 java 中设置标头以接受 Access-Control-Allow-Or

        时间:2023-06-27
        1. <small id='FtMER'></small><noframes id='FtMER'>

            <tbody id='FtMER'></tbody>
          <legend id='FtMER'><style id='FtMER'><dir id='FtMER'><q id='FtMER'></q></dir></style></legend>
          <i id='FtMER'><tr id='FtMER'><dt id='FtMER'><q id='FtMER'><span id='FtMER'><b id='FtMER'><form id='FtMER'><ins id='FtMER'></ins><ul id='FtMER'></ul><sub id='FtMER'></sub></form><legend id='FtMER'></legend><bdo id='FtMER'><pre id='FtMER'><center id='FtMER'></center></pre></bdo></b><th id='FtMER'></th></span></q></dt></tr></i><div id='FtMER'><tfoot id='FtMER'></tfoot><dl id='FtMER'><fieldset id='FtMER'></fieldset></dl></div>
            • <tfoot id='FtMER'></tfoot>

                <bdo id='FtMER'></bdo><ul id='FtMER'></ul>

                  本文介绍了RESTful webservice:如何在 java 中设置标头以接受 Access-Control-Allow-Origin 允许的 XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 RESTful 网络服务,它将返回字符串,它是用 Java (JAX-WS) 编写的.我的问题是当我使用以下 URL 向该 Web 服务发送请求时:

                  I have a RESTful webservice which will return string and it was written in Java (JAX-WS). My problem is when I send request to that webservice with URL like :

                  http://localhost:8080/project/webservices/getlist/getListCustomers

                  在控制台中,它给了我以下错误消息:

                  In the console it's giving me the error message below:

                  XMLHttpRequest 无法加载 url Origin localhost is not allowed通过访问控制允许来源

                  XMLHttpRequest cannot load url Origin localhost is not allowed by Access-Control-Allow-Origin

                  我该如何处理这个问题?

                  How can I handle this issue?

                  Java 代码:

                  @GET
                  @Path("/getsample")
                  public Response getMsg() { 
                      String output = "Jersey say : " ;   
                      return Response.status(200).entity(output).build();
                  }
                  

                  推荐答案

                  阅读这里关于你的 CORS 问题:http://enable-cors.org/

                  Read here about your issue CORS : http://enable-cors.org/

                  检查这是否对您的 getMsg() 方法有帮助:
                  return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();

                  Check if this one help you in your getMsg() method:
                  return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();

                  如果上述方法不起作用,请尝试将 Jersey 过滤器添加到您的服务中.创建过滤器类:

                  If above doesn't work try to add Jersey filter to your service. Create filter class:

                  package your.package;
                  
                  public class CORSFilter implements ContainerResponseFilter {
                  
                      @Override
                      public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
                  
                          cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
                          cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
                          cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
                          cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
                  
                          return cresp;
                      }
                  }
                  

                  稍后注册 win web.xml:

                  And register later win web.xml with:

                  <servlet>
                  <servlet-name>CORS Filter</servlet-name>
                  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                   <init-param>
                      <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
                      <param-value>your.package.CORSFilter</param-value>
                   </init-param>
                  </servlet>
                  <servlet-mapping>
                      <servlet-name>CORS Filter</servlet-name>
                      <url-pattern>/webservices/*</url-pattern>
                  </servlet-mapping>
                  

                  <小时>另一种解决方案是在资源中使用此代码为浏览器提供 OPTIONS.把它放在你有@GET的类中.


                  Another solution is to use this code inside your resource to provide OPTIONS for the browser. Put this in the class where you have @GET.

                    @OPTIONS
                    @Path("/getsample")
                    public Response getOptions() {
                      return Response.ok()
                        .header("Access-Control-Allow-Origin", "*")
                        .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
                        .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With").build();
                    }
                  

                  <小时>如果这不起作用,请尝试将为Access-Control-Allow-Origin"标头提供的 "*" 与您访问此资源的自定义域交换.举例如果您从 http://localhost::8080 调用它,请使用类似 ("Access-Control-Allow-Origin", "http://localhost:8080")("Access-Control-Allow-Origin", "http://localhost:8080") 而不是星号 "*".


                  If non of this work, try to exchange the "*" provided for "Access-Control-Allow-Origin" header with your custom domain where you access this resource. I.g. If you call this from http://localhost::8080 use something like this ("Access-Control-Allow-Origin", "http://localhost:8080") instead of asterisk "*".

                  这篇关于RESTful webservice:如何在 java 中设置标头以接受 Access-Control-Allow-Origin 允许的 XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:问题 Resteasy 3.09 CorsFilter 下一篇:在 Spring 5 Webflux 中启用 CORS?

                  相关文章

                  <legend id='EgPV3'><style id='EgPV3'><dir id='EgPV3'><q id='EgPV3'></q></dir></style></legend>
                  • <bdo id='EgPV3'></bdo><ul id='EgPV3'></ul>

                  1. <tfoot id='EgPV3'></tfoot>

                    1. <i id='EgPV3'><tr id='EgPV3'><dt id='EgPV3'><q id='EgPV3'><span id='EgPV3'><b id='EgPV3'><form id='EgPV3'><ins id='EgPV3'></ins><ul id='EgPV3'></ul><sub id='EgPV3'></sub></form><legend id='EgPV3'></legend><bdo id='EgPV3'><pre id='EgPV3'><center id='EgPV3'></center></pre></bdo></b><th id='EgPV3'></th></span></q></dt></tr></i><div id='EgPV3'><tfoot id='EgPV3'></tfoot><dl id='EgPV3'><fieldset id='EgPV3'></fieldset></dl></div>
                    2. <small id='EgPV3'></small><noframes id='EgPV3'>