如何使用REST API中的文件发布多部分/表单数据

时间:2019-09-05 13:24:22

标签: java rest

我正在尝试编写REST API来上传附件。我已经编写了API。但是,当我尝试从POSTMAN打电话时,它抛出了异常。 REST API:

 @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("attachment") InputStream uploadedInputStream,
            @FormDataParam("attachment") FormDataContentDisposition fileDetail) {
        JSONObject response = new JSONObject();
        JSONObject jobj = new JSONObject();
        if (uploadedInputStream == null) {
            response = StringUtil.getErrorResponse("eleave", jobj, null);
        } else {
            try {
                response.put(Constants.RES_success, true);
                response = companyService.uploadAttachement(uploadedInputStream, fileDetail);
            } catch (JSONException ex) {
                _logger.log(Level.SEVERE, "CompanyRestService.changePassword", ex.getMessage());
                response = StringUtil.getErrorResponse("e01", jobj, ex.getMessage());
            } catch (Exception ex) {
                _logger.log(Level.SEVERE, "CompanyRestService.changePassword", ex.getMessage());
                response = StringUtil.getErrorResponse("erp25", jobj, ex.getMessage());
            }
        }
        return Response.status(200).entity(response.toString()).build();
    }

我尝试了以下屏幕截图,但抛出异常 Img

回复:

org.glassfish.jersey.server.model.ModelValidationException: 
Validation of the application resource model has failed during application initialization.

[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response 
com.krawler.eleave.ws.rest.v1.CompanyRestService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; 
source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], 
producedTypes=[], suspended=false, suspendTimeout=0, 
suspendTimeoutUnit=MILLISECONDS, 
invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.krawler.eleave.ws.rest.v1.CompanyRestService, 
handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@7aa399f8]}, 
handlingMethod=public javax.ws.rs.core.Response 
com.krawler.eleave.ws.rest.v1.CompanyRestService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), 
parameters=[Parameter [type=class java.io.InputStream, source=attachment, defaultValue=null], 
Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=attachment, defaultValue=null]], 
responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']

依赖关系:

<dependencies><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>3.1</version></dependency><dependency><groupId>asm</groupId><artifactId>asm</artifactId><version>3.1</version></dependency><dependency><groupId>com.mycompany</groupId><artifactId>poi-3.5-beta6</artifactId><version>20090622</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1</version></dependency><dependency><groupId>backport-util-concurrent</groupId><artifactId>backport-util-concurrent</artifactId><version>3.1</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.3</version></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.1</version></dependency><dependency><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId><version>2.0</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.1</version></dependency><dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>1.4</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.4</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging-api</artifactId><version>1.0.4</version></dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>1.1</version></dependency><dependency><groupId>net.sf.ezmorph</groupId><artifactId>ezmorph</artifactId><version>1.0.4</version></dependency><dependency><groupId>net.sf.flexjson</groupId><artifactId>flexjson</artifactId><version>3.0</version></dependency><dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>build210</version></dependency><dependency><groupId>org.mnode.ical4j</groupId><artifactId>ical4j</artifactId><version>1.0.3</version></dependency><dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version></dependency><dependency><groupId>oro</groupId><artifactId>oro</artifactId><version>2.0.8</version></dependency><dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>4.1.2</version></dependency><dependency><groupId>com.octo.captcha</groupId><artifactId>jcaptcha-all</artifactId><version>1.0-RC6</version></dependency><dependency><groupId>jdom</groupId><artifactId>jdom</artifactId><version>1.1</version></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>jetty</artifactId><version>6.1.6</version></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>jetty-util</artifactId><version>6.1.6</version></dependency><dependency><groupId>com.uwyn</groupId><artifactId>jhighlight</artifactId><version>1.0</version></dependency><dependency><groupId>jmimemagic</groupId><artifactId>jmimemagic</artifactId><version>0.0.4a</version></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.4</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></dependency><dependency><groupId>com.mycompany</groupId><artifactId>KrawlerJsonLib</artifactId><version>1.0</version></dependency><dependency><groupId>com.mycompany</groupId><artifactId>krawlersso</artifactId><version>1.2</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>1.9.1</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></dependency><dependency><groupId>org.tmatesoft.svnkit</groupId><artifactId>svnkit</artifactId><version>1.3.1</version></dependency><dependency><groupId>com.paypal.sdk</groupId><artifactId>paypal-core</artifactId><version>1.4.3</version></dependency><dependency><groupId>pdfbox</groupId><artifactId>pdfbox</artifactId><version>0.7.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.7</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-contrib</artifactId><version>3.7-beta3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.7</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.7</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.7</version></dependency><dependency><groupId>rome</groupId><artifactId>rome</artifactId><version>1.0</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>org.tuckey</groupId><artifactId>urlrewritefilter</artifactId><version>3.2.0</version></dependency><dependency><groupId>xerces</groupId><artifactId>xercesImpl</artifactId><version>2.8.0</version></dependency><dependency><groupId>org.apache.xmlbeans</groupId><artifactId>xmlbeans</artifactId><version>2.6.0</version></dependency><dependency><groupId>xom</groupId><artifactId>xom</artifactId><version>1.1</version></dependency><dependency><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet</artifactId><version>${jersey-version}</version><exclusions><exclusion><groupId>org.glassfish.hk2.external</groupId><artifactId>asm-all-repackaged</artifactId></exclusion><exclusion><groupId>org.glassfish.hk2.external</groupId><artifactId>cglib</artifactId></exclusion></exclusions></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-json-jackson</artifactId><version>${jersey-version}</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.6.0</version></dependency><dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId><version>1.1</version></dependency><dependency><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-multipart</artifactId><version>${jersey-version}</version></dependency></dependencies>

1 个答案:

答案 0 :(得分:0)

从例外情况来看,我可以告诉您正在使用Jersey2.x。但是,您似乎正在使用com.sun.jersey.core.header.FormDataContentDisposition,它属于 old Jersey 1.x。

您想改用org.glassfish.jersey.media.multipart.FormDataContentDisposition

首先删除jersey-multipart依赖项:

<!-- Multipart support for Jersey 1.x -->
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>${jersey.version}</version>
</dependency>

然后添加jersey-media-multipart依赖项:

<!-- Multipart support for Jersey 2.x -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey.version}</version>
</dependency>

修复导入。并在MultiPartFeatureResourceConfig子类中注册Application