JAX-RS Multipart 与 com.sun.jersey [英] JAX-RS Multipart with com.sun.jersey

查看:41
本文介绍了JAX-RS Multipart 与 com.sun.jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Karaf 中托管了一个 REST 服务,它可以正常处理除多部分请求之外的所有请求.我正在使用 com.sun.jersey 包,因为我只成功地在 Karaf 内部托管了这些,以便通过 HTTP 访问.

I have a REST service hosted inside Karaf, which is working fine with all requests except for multipart requests. I'm using the com.sun.jersey packages, as I have only succeeded in hosting these inside of Karaf to be accessed over HTTP.

当我尝试接收HttpServletRequest 里面的 POST 和调用 getParts() 方法,我得到错误:

When I try to receive the HttpServletRequest inside the POST and call the getParts() method on it, I get the error:

IllegalStateException: servlet 没有多部分配置

IllegalStateException: No multipart config for servlet

我发现我缺少 @MultipartConfig 注释在我的 servlet 上,所以我将它添加到我正在使用的 servlet 实现中.我扩展 com.sun.jersey.spi.container.servlet.ServletContainer 并将注释添加到该类.但这不起作用.

I have found that I am missing the @MultipartConfig annotation on my servlet, so I added this to the servlet implementation I am using. I extend com.sun.jersey.spi.container.servlet.ServletContainer and add the annotation to that class. But this does not work.

我也试过使用我自己的 HttpServlet 类扩展,它重现了错误:

I've also tried using my own extension of the HttpServlet class, that reproduces the error:

@MultipartConfig
public class MultipartServlet extends HttpServlet {

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

        try {
            final HttpServletRequest httpRequest = (HttpServletRequest)request;
            final Collection<Part> parts = httpRequest.getParts();

            System.out.println("There are " + parts.size() + " parts");
        }
        catch (Exception exception) {
            System.out.println("MEGA FAIL");
            System.out.println(exception.getMessage());
        }

        super.service(request, response);
    }
}

我见过使用 org.glassfish.jersey 的方法 使用 ResourceConfig 注册 MultiPartFeature 类的包code>,但我无法在 Karaf 内部通过 HTTP 访问这些包(服务似乎注册没有错误,但所有请求都返回 404 响应).

I've seen the approach using org.glassfish.jersey packages that makes registers the MultiPartFeature class with the ResourceConfig, but I haven't been able to get these packages accessible over HTTP inside of Karaf (the services appear to register without error, but all requests return 404 responses).

推荐答案

与其尝试使用 Servlet multipart,您只需使用 泽西岛的多部分支持.在链接中的示例中,它使用命名部件.如果您希望能够处理所有未知部分,您可以使用 FormDataMultiPart 作为方法参数.这样您就可以使用 getFields()

Instead of trying to use the Servlet multipart, you just use Jersey's multipart support. In the example in the link, it uses named parts. If you want to be able to process all unknown parts, you can just use FormDataMultiPart as the method parameter. This way you can access all the parts with getFields()

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response post(FormDataMultiPart multiPart) {
    final Map<String, List<FormDataBodyPart>> = multiPart.getFields();
}

这篇关于JAX-RS Multipart 与 com.sun.jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆