泽西岛:如何注册MultiPartConfigProvider类(Jersey: How to re

2019-10-20 05:35发布

我试图加载具有球衣多个文件,但它要求我一些配置,我无法找到。 这是依赖,我不得不添加,因为球衣无法解析器类型“多部分/相关”:

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

这是我用的是多的API:

    @PUT
    @Consumes("multipart/related")
    @Path("/{id}/user/{userId}")
    public Response loadFile(@PathParam("id") int id, @PathParam("userId") int userId, MultiPart files) throws Exception{
        int resultado = tool.loadFile(id, userId, files);
        switch (resultado){
        case 401:   return Response.status(401).entity("No existe el experimento con id: "+id).build();
        case 402:   return Response.status(402).entity("El tipo de uno de los archivos no se encuentra en los tipos de entregables del experimento").build();
        case 403:   return Response.status(403).entity("El usuario: "+userId+" no participa en el experimento").build();
        case 405:   return Response.status(405).entity("Debe ingresar un comentario").build();
        case 406:   return Response.status(406).entity("Hay entregables sin subir").build();
        case 200:   return Response.status(200).build();
        }
        return null;
    }

这是web.xml中,我想我在这里失去了一些东西:

    <?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.mockapi</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

这是个例外:

A MultiException has 3 exceptions.  They are:
1. java.lang.IllegalArgumentException: The MultiPartConfig instance we expected is not present.  Have you registered the MultiPartConfigProvider class?
2. java.lang.IllegalStateException: Unable to perform operation: create on com.sun.jersey.multipart.impl.MultiPartReaderClientSide
3. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory

Answer 1:

如果我们使用dropwizard的新版本,则相关性应该是, <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.15</version> </dependency>由于新版本dropwizard使用org.glassfish.jersey.core:jersey-server:jar
我们必须要注册MultiPartFeature在我们的应用程序的run方法environment.jersey().register(MultiPartFeature.class);
欲了解更多信息,您可以检查在这里



Answer 2:

尽量把这个你的pom.xml

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

它解决我的问题,我希望它



文章来源: Jersey: How to register MultiPartConfigProvider class