当前位置: 首页 > 知识库问答 >
问题:

Java语言jersey rest api的文件上载和其他资源中的lang.AbstractMethodError

叶光华
2023-03-14

在我们的应用程序中,我们希望通过android使用rest api上传文件和有关文件的信息。这是我的pom。xml

<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.arpit.restapi.megabizz</groupId>
<artifactId>Megabizz</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Megabizz</name>

<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

 <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.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>

        <version>1.7.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.8</version>
    </dependency>



</dependencies>

<build>
    <finalName>Megabizz</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>
        <plugin>
          <groupId>com.heroku.sdk</groupId>
          <artifactId>heroku-maven-plugin</artifactId>
          <version>0.4.4</version>
        </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>


<properties>
    <jersey.version>2.16</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

这是我的jersey资源文件

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/notes")
public class Notes{
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String fileUpload(@FormDataParam("file")InputStream uploadedInputStream,
        @FormDataParam("file")FormDataContentDisposition fileDetial)
{

    String uploadedFileLocation="D:\\megabizz"+fileDetial.getFileName();
    writeToFile(uploadedInputStream, uploadedFileLocation);
    return"file successfully uploaded";
}
private void writeToFile(InputStream uploadedInputStream,String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(
                    uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}

有错误的日志

        23, 2015 1:08:51 AM org.apache.catalina.core.StandardWrapperValve invoke
        SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/Megabizz] threw exception [Servlet execution threw an exception] with root cause
        java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
        at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:289)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
        at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)

我没有弄错什么,在添加最后3个依赖项后,我无法访问我项目的任何资源。即使我没有上传文件并尝试访问其他资源,那么我也会收到这个错误。请帮我解决这个问题。

共有1个答案

傅俊德
2023-03-14

不要混用球衣的主要版本。它们不兼容,即

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.8</version>
</dependency>

把那两个人干掉。对于多部分支持,请使用

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.16</version>
</dependency>

这是泽西2. x支持。然后您需要注册MultiPartFeature。这取决于您如何配置您的应用程序,以及如何做到这一点。这些链接可能会有所帮助:链接、链接、链接

 类似资料:
  • 了解更多有关于Giojs 基本元素 阅读markdown格式的API文档 APIs 如果你有兴趣参与Giojs开发,不妨先从 开发指南 中熟悉一下Giojs的开发规范

  • TensorFlow 白皮书 在这份白皮书里,你可以找到关于 TensorFlow 编程模型的更多详情和 TensorFlow 的实现原理。 TensorFlow: Large-scale machine learning on heterogeneous systems 引用 如果你在你的研究中使用了 TensorFlow,并且希望在引用中注记 TensorFlow,我们建议你引用上面这篇论文。

  • Nacos社区当前仅提供了Java版本的客户端,我们将主要依靠社区的贡献来发展多语言客户端。在未来,我们将向Nacos社区用户推荐那些最被广泛使用的以及支持最好的客户端作为Nacos相应语言的官方版本。 go cpp python nodejs more ...

  • Cheatsheets PHP Cheatsheets - for variable comparisons, arithmetics and variable testing in various PHP versions PHP Security Cheatsheet 更多最佳实践 PHP Best Practices Best practices for Modern PHP Develop

  • 这是一种向可观察的客户列表中添加新行星的方法。 我想知道我是否正确使用资源尝试,以及自动关闭是否工作。 我的问题是,这部分需要被封闭在一个try-catch块中,还是自动关闭。

  • 我知道已经有一些关于相对路径的问题,但是我总是无法让JavaFX FXML加载器从包本身以外的包加载资源。 加载类位于包gui.controllers中,fxml文件位于包中。我现在应该写什么: 提前谢谢