javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath

huangapple go评论54阅读模式
英文:

javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath

问题

当我调用S3Client中的listObjects方法时,它抛出以下错误。

> javax.xml.bind.JAXBException: 在模块路径或类路径上未找到JAXB-API的实现。 - 带有链接的异常:
> [java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
> javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException:
> 在模块路径或类路径上未找到JAXB-API的实现。
> 带有链接的异常:[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: 在模块路径或类路径上未找到JAXB-API的实现。
> 带有链接的异常:[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

我尝试在build.gradle中添加jaxb,但没有帮助。

	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'

列出方法

    public void list() {
        ListObjectsResult objectListing = s3client.listObjects(nBucket);//这一行引起了错误

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

    }

我该如何解决这个问题?或者,有没有其他列出所有键的方法?

英文:

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?

答案1

得分: 2

使用AWS SDK for Java v2的S3Client。官方的AWS SDK for Java V2 S3Client在Spring BOOT web项目中表现出色。使用AWS Java的S3Client,您可以避免所有这些依赖问题。

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]

/**
 * 在运行此Java V2示例代码之前,请设置好您的开发环境,包括您的凭据。
 *
 * 有关更多信息,请参阅以下文档主题:
 *
 * 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 - 从中读取对象的Amazon S3存储桶。 \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();

            List<S3Object> objects = s3.listObjects(listObjects).contents();
            for (S3Object myValue : objects) {
                System.out.print("\n 键的名称是 " + myValue.key());
                System.out.print("\n 对象大小为 " + calKb(myValue.size()) + " KB");
                System.out.print("\n 拥有者是 " + myValue.owner());
            }

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

    //将字节转换为千字节(KB)。
    private static long calKb(Long val) {
        return val / 1024;
    }
   // snippet-end:[s3.java2.list_objects.main]
}
英文:

USe AWS SDK for Java v2 S3Client. The official AWS SDK for Java V2 S3Client works perfectly in a Spring BOOT web project. Using the AWS Java S3Client, you can avoid all of these dependency issues.

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 = &quot;\n&quot; +
&quot;Usage:\n&quot; +
&quot;    &lt;bucketName&gt; \n\n&quot; +
&quot;Where:\n&quot; +
&quot;    bucketName - The Amazon S3 bucket from which objects are read. \n\n&quot; ;
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&lt;S3Object&gt; objects = res.contents();
for (S3Object myValue : objects) {
System.out.print(&quot;\n The name of the key is &quot; + myValue.key());
System.out.print(&quot;\n The object is &quot; + calKb(myValue.size()) + &quot; KBs&quot;);
System.out.print(&quot;\n The owner is &quot; + 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]
}

答案2

得分: 0

更好的做法是使用官方的AWS SDK,而不是第三方API。

英文:

It’s always better to use an official AWS SDK instead of 3rd party APIs.

huangapple
  • 本文由 发表于 2023年2月6日 16:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定