服务器异常:javax.xml.bind.JAXBException:在模块路径或类路径中未找到JAXB-API的实现

zz2j4svz  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(401)

I am using com.emc.object.s3. I am trying to list all the key in a S3 bucket, but when I call the listObjects method in S3Client, it raises the following error.
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.bind.v2.ContextFactory] javax.ws.rs.WebApplicationException: 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.bind.v2.ContextFactory] at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:115) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634) ... 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.bind.v2.ContextFactory] at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:232) at javax.xml.bind.ContextFinder.find(ContextFinder.java:375) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getStoredJAXBContext(AbstractJAXBProvider.java:196) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:188) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:140) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:123) at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111) ... 96 more Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:72) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220) at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92) at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230) ... 104 more
I tried to add jaxb to build.gradle, but it doesn't help.

implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.0-b170127.1453'
    implementation 'javax.activation:activation:1.1'
    implementation 'javax.xml.bind:jaxb-api:2.3.0'

listing method

public void list() {
        ListObjectsResult objectListing = s3client.listObjects(nBucket);//this line causing the error

        for (S3Object objectSummary : objectListing.getObjects()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() + ")");
        }

    }

How can I fix the problem? Alternatively, any other methods to list all keys?

62lalag4

62lalag41#

官方的AWS SDK for Java V2 S3Client可以在Sping Boot web项目中完美运行。使用AWS JavaS3Client,您可以避免所有这些依赖性问题。

package com.example.s3;

// snippet-start:[s3.java2.list_objects.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import java.util.List;
// snippet-end:[s3.java2.list_objects.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class ListObjects {

    public static void main(String[] args) {

        final String usage = "\n" +
            "Usage:\n" +
            "    <bucketName> \n\n" +
            "Where:\n" +
            "    bucketName - The Amazon S3 bucket from which objects are read. \n\n" ;

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String bucketName = args[0];
        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
            .region(region)
            .credentialsProvider(credentialsProvider)
            .build();

        listBucketObjects(s3, bucketName);
        s3.close();
    }

    // snippet-start:[s3.java2.list_objects.main]
    public static void listBucketObjects(S3Client s3, String bucketName ) {

        try {
            ListObjectsRequest listObjects = ListObjectsRequest
                .builder()
                .bucket(bucketName)
                .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();
            for (S3Object myValue : objects) {
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
            }

        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }

    //convert bytes to kbs.
    private static long calKb(Long val) {
        return val/1024;
    }
   // snippet-end:[s3.java2.list_objects.main]
}
6ss1mwsb

6ss1mwsb2#

最好使用官方AWS SDK而不是第三方API。

相关问题