0

I created normal GET and POST method apd using java. for checking I tried postman and java client, the output got good. But When I used in AJAX in a normal HTML page, it shows the below error message in console enter image description here

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.
Ajax.html:23 GET 'http://localhost:****/*****/' net::ERR_FAILED
(anonymous) @ Ajax.html:23
Ajax.html:26 error TypeError: Failed to fetch
Ajax.html:23 Fetch failed loading: GET 'http://localhost:****/*****/'.

In Tomcat also shows exception(Multiexception)

org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
atorg.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:144)
...............................
MultiException stack 2 of 2
java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:393)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
at..................

WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 4
javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
at org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:144)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at......................
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:177)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:364)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:508)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:465)
at.....................

MultiException stack 2 of 4
java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:393)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
at ...............
MultiException stack 3 of 4
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler errors were found
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:247)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.glassfish.jersey.hk2.RequestContext.findOrCreate(RequestContext.java:83)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
at.........................
MultiException stack 4 of 4
java.lang.IllegalStateException: Unable to perform operation: resolve on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:387)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.glassfish.jersey.hk2.RequestContext.findOrCreate(RequestContext.java:83)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
at..............


Aug 21, 2020 3:01:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/onlineshop] threw exception [A MultiException has 4 exceptions.  They are:
1. javax.ws.rs.ProcessingException: Error creating a JAXBContext for wadl processing.
2. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor$OptionsHandler
] with root cause
java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at.................... 

The below code is javascript

<script>

        var myHeaders = new Headers();
        myHeaders.append("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbElkIjoidXNlcjFAZWthcnQuY29tIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9.H_LMXrlp8gNCvXN7IRtLwQiTcSMyRczaSyuDfv8mlho");
        myHeaders.append("Access-Control-Allow-Origin", "http://localhost:****/ ");
        var requestOptions = {
            // mode: 'no-cors',
            method: 'GET',
            headers: myHeaders,
            redirect: 'follow'
        };

        fetch("http://localhost:9090/onlineshop/", requestOptions)
            .then(response => response.text())
            .then(result => console.log(result))
            .catch(error => console.log('error', error));
    </script>

if I uncomment the mode (mode: 'no-cors') it not shows the error and exceptions but GET fetch not working.

Can you explain what is cors and no-cors? and what is the purpose of it?

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>onlineshop-ajith-rest</groupId>
    <artifactId>onlineshop</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineshop</name>

    <build>
        <finalName>onlineshop</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

     <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.26</version>
        </dependency>
        
         <dependency>
             <groupId>javax.activation</groupId>
             <artifactId>activation</artifactId>
             <version>1.1.1</version>
         </dependency>
        <!-- uncomment this to get JSON support-->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            </dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        
        <dependency> 
           <groupId>com.google.code.gson</groupId> 
           <artifactId>gson</artifactId> 
           <version>2.6.2</version> 
       </dependency>
       
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth2-client</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.10.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.2</version>
        </dependency>
                
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>
        
    </dependencies>
    <properties>
        <jersey.version>2.26-b03</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

FYI it only shows the error in browser when used AJAX

0 Answers0