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

时间:2017-07-05 09:28:16

标签: java maven jax-rs

我在上传文件功能的创建方法POST时发现错误。如何解决这个问题呢?感谢。

此日志错误:

  

2017年7月5日下午4:20:23 org.apache.coyote.AbstractProtocol init INFO:   初始化ProtocolHandler [" http-bio-8087"] 2017年7月5日下午4:20:23   org.apache.catalina.core.StandardService startInternal INFO:正在启动   服务Tomcat Jul 05,2017 4:20:23 PM   org.apache.catalina.core.StandardEngine startInternal INFO:正在启动   Servlet引擎:Apache Tomcat / 7.0.47 Jul 05,2017 4:20:27 PM   com.sun.jersey.api.core.PackagesResourceConfig init INFO:正在扫描   包中的根资源和提供者类:
  org.arpit.java2blog.controller 2017年7月5日下午4:20:28   com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO:Root   找到的资源类:class   org.arpit.java2blog.controller.FileUploadController类   org.arpit.java2blog.controller.CountryController类   org.arpit.java2blog.controller.KasusController 2017年7月5日下午4:20:28   com.sun.jersey.api.core.ScanningResourceConfig init INFO:没有提供者   找到的课程。 2017年7月5日下午4:20:28   com.sun.jersey.server.impl.application.WebApplicationImpl _initiate   信息:启动泽西岛应用程序,版本'泽西岛:1.19.3   2016/10/24 03:43' 2017年7月5日下午4:20:29   com.sun.jersey.spi.inject.Errors processErrorMessages SEVERE:The   已使用资源和/或检测到以下错误和警告   提供程序类:SEVERE:缺少方法public的依赖项   javax.ws.rs.core.Response   org.arpit.java2blog.controller.FileUploadController.uploadFile(java.io.InputStream中,org.glassfish.jersey.media.multipart.FormDataContentDisposition)   at index at index 0 SEVERE:缺少方法public的依赖项   javax.ws.rs.core.Response   org.arpit.java2blog.controller.FileUploadController.uploadFile(java.io.InputStream中,org.glassfish.jersey.media.multipart.FormDataContentDisposition)   at index at index 1 SEVERE:Method,public   javax.ws.rs.core.Response   org.arpit.java2blog.controller.FileUploadController.uploadFile(java.io.InputStream中,org.glassfish.jersey.media.multipart.FormDataContentDisposition),   用资源,类的POST注释   无法识别org.arpit.java2blog.controller.FileUploadController   作为有效的资源方法。 2017年7月5日下午4:20:29   org.apache.catalina.core.ApplicationContext log SEVERE:   StandardWrapper.Throwable   com.sun.jersey.spi.inject.Errors $ ErrorMessagesException at   com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)     在com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)at   com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)     在   com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)     在   com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)     在   com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)     在   com.sun.jersey.spi.container.servlet.ServletContainer $ InternalWebComponent.initiate(ServletContainer.java:339)     在   com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)     在   com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)     在   com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)     在   com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)     在javax.servlet.GenericServlet.init(GenericServlet.java:160)at   org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:748)

     

2017年7月5日下午4:20:29 org.apache.catalina.core.StandardContext   loadOnStartup SEVERE:Servlet / JSONExampleJAXB引发了load()异常   com.sun.jersey.spi.inject.Errors $ ErrorMessagesException at   com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)     在com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)at   com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)     在   com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)     在   com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)     在   com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)     在   com.sun.jersey.spi.container.servlet.ServletContainer $ InternalWebComponent.initiate(ServletContainer.java:339)     在   com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)     在   com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)     在   com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)     在   com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)     在javax.servlet.GenericServlet.init(GenericServlet.java:160)at   org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:748)

我的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>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>

我的web.xml

<?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();  
        }   

}

2 个答案:

答案 0 :(得分:0)

更新你的pom.xml。请帮助你

<dependency>
    <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
      <scope>provided</scope>
  </dependency>

答案 1 :(得分:0)

这是我的新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>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.8</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.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</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>1.19.3</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>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</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>

        <!-- JBoss -->
        <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>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.6</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>