在服务器中检测到未知hk2故障,并且在使用ajax时请求的资源上不存在“access control allow origin”标头

ybzsozfc  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(488)

我使用java创建了普通的get和post方法apd。为了检查,我尝试了postman和java客户机,结果很好。但是当我在一个普通的html页面中使用ajax时,它在console enter image description中显示了下面的错误消息

  1. Access to fetch at 'http://localhost:****/*****/' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  2. Ajax.html:23 GET 'http://localhost:****/*****/' net::ERR_FAILED
  3. (anonymous) @ Ajax.html:23
  4. Ajax.html:26 error TypeError: Failed to fetch
  5. Ajax.html:23 Fetch failed loading: GET 'http://localhost:****/*****/'.

在tomcat中还显示异常(multiexception)

  1. org.glassfish.jersey.internal.Errors logErrors
  2. WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
  3. MultiException stack 1 of 2
  4. javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
  5. atorg.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:144)
  6. ...............................
  7. MultiException stack 2 of 2
  8. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
  9. at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:393)
  10. at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
  11. at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
  12. at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
  13. at..................
  14. WARNING: Unknown HK2 failure detected:
  15. MultiException stack 1 of 4
  16. javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
  17. at org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:144)
  18. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  19. at......................
  20. Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
  21. - with linked exception:
  22. [java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
  23. at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:177)
  24. at javax.xml.bind.ContextFinder.find(ContextFinder.java:364)
  25. at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:508)
  26. at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:465)
  27. at.....................
  28. MultiException stack 2 of 4
  29. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
  30. at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:393)
  31. at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
  32. at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
  33. at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
  34. at ...............
  35. MultiException stack 3 of 4
  36. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler errors were found
  37. at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:247)
  38. at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)
  39. at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
  40. at org.glassfish.jersey.hk2.RequestContext.findOrCreate(RequestContext.java:83)
  41. at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
  42. at.........................
  43. MultiException stack 4 of 4
  44. java.lang.IllegalStateException: Unable to perform operation: resolve on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler
  45. at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:387)
  46. at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
  47. at org.glassfish.jersey.hk2.RequestContext.findOrCreate(RequestContext.java:83)
  48. at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
  49. at..............
  50. Aug 21, 2020 3:01:36 PM org.apache.catalina.core.StandardWrapperValve invoke
  51. SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/onlineshop] threw exception [A MultiException has 4 exceptions. They are:
  52. 1. javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
  53. 2. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
  54. 3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler errors were found
  55. 4. java.lang.IllegalStateException: Unable to perform operation: resolve on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler
  56. ] with root cause
  57. java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
  58. at....................

下面的代码是javascript

  1. <script>
  2. var myHeaders = new Headers();
  3. myHeaders.append("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbElkIjoidXNlcjFAZWthcnQuY29tIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9.H_LMXrlp8gNCvXN7IRtLwQiTcSMyRczaSyuDfv8mlho");
  4. myHeaders.append("Access-Control-Allow-Origin", "http://localhost:****/ ");
  5. var requestOptions = {
  6. // mode: 'no-cors',
  7. method: 'GET',
  8. headers: myHeaders,
  9. redirect: 'follow'
  10. };
  11. fetch("http://localhost:9090/onlineshop/", requestOptions)
  12. .then(response => response.text())
  13. .then(result => console.log(result))
  14. .catch(error => console.log('error', error));
  15. </script>

如果我取消对模式的注解(mode:'no cors'),它不会显示错误和异常,但是get fetch不起作用。
你能解释什么是cors和没有cors吗?它的目的是什么?
pom.xml文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>onlineshop-ajith-rest</groupId>
  5. <artifactId>onlineshop</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>onlineshop</name>
  9. <build>
  10. <finalName>onlineshop</finalName>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <version>2.5.1</version>
  16. <inherited>true</inherited>
  17. <configuration>
  18. <source>1.7</source>
  19. <target>1.7</target>
  20. </configuration>
  21. </plugin>
  22. </plugins>
  23. </build>
  24. <dependencyManagement>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.glassfish.jersey</groupId>
  28. <artifactId>jersey-bom</artifactId>
  29. <version>${jersey.version}</version>
  30. <type>pom</type>
  31. <scope>import</scope>
  32. </dependency>
  33. </dependencies>
  34. </dependencyManagement>
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.glassfish.jersey.containers</groupId>
  38. <artifactId>jersey-container-servlet-core</artifactId>
  39. <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
  40. <!-- artifactId>jersey-container-servlet</artifactId -->
  41. </dependency>
  42. <dependency>
  43. <groupId>org.glassfish.jersey.inject</groupId>
  44. <artifactId>jersey-hk2</artifactId>
  45. <version>2.26</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>javax.activation</groupId>
  49. <artifactId>activation</artifactId>
  50. <version>1.1.1</version>
  51. </dependency>
  52. <!-- uncomment this to get JSON support-->
  53. <dependency>
  54. <groupId>org.glassfish.jersey.media</groupId>
  55. <artifactId>jersey-media-json-jackson</artifactId>
  56. </dependency>
  57. <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
  58. <dependency>
  59. <groupId>javax.xml.bind</groupId>
  60. <artifactId>jaxb-api</artifactId>
  61. <version>2.3.0</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>com.google.code.gson</groupId>
  65. <artifactId>gson</artifactId>
  66. <version>2.6.2</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.glassfish.jersey.security</groupId>
  70. <artifactId>oauth2-client</artifactId>
  71. </dependency>
  72. <dependency>
  73. <groupId>com.auth0</groupId>
  74. <artifactId>java-jwt</artifactId>
  75. <version>3.10.3</version>
  76. </dependency>
  77. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
  78. <dependency>
  79. <groupId>com.fasterxml.jackson.core</groupId>
  80. <artifactId>jackson-annotations</artifactId>
  81. <version>2.11.2</version>
  82. </dependency>
  83. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  84. <dependency>
  85. <groupId>com.fasterxml.jackson.core</groupId>
  86. <artifactId>jackson-databind</artifactId>
  87. <version>2.11.0</version>
  88. </dependency>
  89. </dependencies>
  90. <properties>
  91. <jersey.version>2.26-b03</jersey.version>
  92. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  93. </properties>
  94. </project>

仅供参考它只显示在浏览器中使用ajax时的错误

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题