带有com.sun.jersey的JAX-RS Multipart [英] JAX-RS Multipart with com.sun.jersey

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

问题描述

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

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中,然后调用

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 软件包的方法, MultiPartFeature 类和ResourceConfig,但是我无法通过HTTP来访问这些包. Karaf(服务似乎已正确注册,但所有请求均返回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多部分功能,而只需使用

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

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

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