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

缺少方法public javax.ws.rs.core.response(上载文件JAX-RS)的依赖项

孟韬
2023-03-14

我在为上载文件函数创建方法POST时发现了一个错误。如何解决这个问题?多谢了。

此日志错误:

<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>com.project</groupId>
    <artifactId>JSONExampleJAXB</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>JSONExampleJAXB Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jersey.version>1.19.3</jersey.version>
        <java-version>1.8</java-version>
        <org.springframework-version>4.3.8.RELEASE</org.springframework-version>
        <jackson.version>2.7.5</jackson.version>
        <jaxb-api.version>2.2.11</jaxb-api.version>
        <hibernate.version>4.3.11.Final</hibernate.version>
        <slf4j.version>1.7.21</slf4j.version>           
    </properties>

    <dependencies>
        <!-- Local Libraries -->
        <dependency>
            <groupId>com.sap.hana</groupId>
            <artifactId>ng</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/ngdbc-1.0.jar</systemPath>
        </dependency>

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

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1-m05</version>
            <scope>provided</scope>
        </dependency>

        <!-- Dependency untuk mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>

        <!-- JSON Dependency -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>${jersey.version}</version>
        </dependency>       

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>   

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>   

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- Mimepull untuk prerequisites upload file -->
        <dependency>
            <groupId>org.jvnet.mimepull</groupId>
            <artifactId>mimepull</artifactId>
            <version>1.9.4</version>
        </dependency>

        <!--  -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.2.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>2.2.0.GA</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jettison-provider</artifactId>
            <version>2.2.0.GA</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.10</version>
        </dependency>

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

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jdk-http</artifactId>
            <version>2.10</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>JSONExampleJAXB</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8087</port>
                    <path>/JSONExampleJAXB</path>
                </configuration>
            </plugin>
        </plugins>

    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>org.arpit.java2blog.controller</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>org.arpit.java2blog.config.CORSFilter</param-value>
        </init-param>

        <init-param>  
            <param-name>org.arpit.java2blog.controller.FileUploadController</param-name>  
            <param-value>org.glassfish.jersey.filter.LoggingFilter;  
             org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>  
        </init-param>       

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>  

FileUploadController.java

package org.arpit.java2blog.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Path("/files") 
public class FileUploadController {

    @POST  
    @Path("/upload")  
    @Consumes(MediaType.MULTIPART_FORM_DATA)  
    public Response uploadFile(  
            @FormDataParam("file") InputStream uploadedInputStream,  
            @FormDataParam("file") FormDataContentDisposition fileDetail) {  
            String fileLocation = "d://" + fileDetail.getFileName();  
                    //saving file  
            try {  
                FileOutputStream out = new FileOutputStream(new File(fileLocation));  
                int read = 0;  
                byte[] bytes = new byte[1024];  
                out = new FileOutputStream(new File(fileLocation));  
                while ((read = uploadedInputStream.read(bytes)) != -1) {  
                    out.write(bytes, 0, read);  
                }  
                out.flush();  
                out.close();  
            } catch (IOException e) {e.printStackTrace();}  
            String output = "File successfully uploaded to : " + fileLocation;  
            return Response.status(200).entity(output).build();  
        }   

}

共有1个答案

朱高超
2023-03-14

更新pom.xml。应该会帮你的

<dependency>
    <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
      <scope>provided</scope>
  </dependency>
 类似资料:
  • 我需要上传文件: 我有错误: 2017-04-11 23:22:37.416错误---[Thread-12]grails。靴子GrailsApp:编译错误:启动失败:类生成期间的常规错误:java。lang.NoClassDefFoundError:无法加载类org。springframework。网状物多部分。平民CommonsMultipartFile由于缺少依赖项Lorg/apache/co

  • 如果我在我的应用程序中包含以下J2EE依赖项,我就可以访问servlet和JAX-RS类和接口: 但是它们是从哪里来的呢?在Maven Central上,我看到作为依赖项,这又作为依赖项和,所以从表面上看,这就是答案。 但是所有这些依赖项都被标记为,这意味着作为传递依赖项,除非我明确包含它们,否则它们不会在我的项目中显示。但我的程序编译时只需依赖。为什么? 这是我的依赖树;我不知道他们从哪里进来:

  • 在这小节中将演示如何通过JAX-RS API下载java中的文本文件,图像文件,pdf文件和excel文件。 为此需要编写几行代码。 在这里使用 jersey 实现来开发JAX-RS文件下载示例。 首先打开Eclipse,创建一个动态Web项目:JaxRsFileDownload 。项目结构如下图所示 - Jersey Jar文件下载网址:https://jersey.github.io/down

  • 问题内容: 我正在尝试使用Maven构建Javahibernate项目。但是,当我尝试执行此操作时,似乎没有依赖项吗? 我的项目中现在有了这个pom.xml: 当我尝试使用mvn进行构建时,出现此错误: 我搜索了此错误,发现很多人必须在其pom.xml文件中更改其hibernate核心依赖版本,但似乎我没有hibernate核心,那么如何解决此问题? 问题答案: 它具有建议的JTA 1.1依赖性。

  • 我在构建我的应用程序时遇到此问题。有人知道怎么回事吗? React Hook useEffect缺少依赖项:“conectar”。包括它或删除依赖项数组react hooks/dep

  • 在我的反应/redux应用程序中,我使用调度来调用每次安装组件时从redux中的状态检索数据的操作。问题正在使用状态发生我的方式不工作 下面是我得到的错误: React Hook useEffect缺少依赖项:“dispatch”。包括它或删除依赖项数组。像“getInvoiceData”这样的外部作用域值不是有效的依赖项,因为对它们进行变异不会重新呈现组件react HOOK/DEP 这是我的密