Accepting a List as a parameter to a Jersey webservice that consumes a content type of multi-part

前提是你 提交于 2019-12-06 02:34:29

Having stepped through some more of the jersey code, my conclusion is that I can not have a parameter of type List on my method when using multi-part. At one point in the process Jersey loops through each paramter on the method finding an Injectable to read the value for each parameter (sorry probably not a great explaination, but I have debugged as much as I needed to), within the class com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider in the getInjectables method is the following code:

 private List<Injectable> getInjectables(AbstractResourceMethod method) {
    List<Injectable> list = new ArrayList<Injectable>(method.getParameters().size());
    for (int i = 0; i < method.getParameters().size(); i++) {
        Parameter p = method.getParameters().get(i);
        if (Parameter.Source.ENTITY == p.getSource()) {
            if (FormDataMultiPart.class.isAssignableFrom(p.getParameterClass())) {
                list.add(new FormDataMultiPartInjectable());
            } else {
                list.add(null);
            }
        } else if (p.getAnnotation().annotationType() == FormDataParam.class) {
            if (Collection.class == p.getParameterClass() || List.class == p.getParameterClass()) {
                Class c = ReflectionHelper.getGenericClass(p.getParameterType());
                if (FormDataBodyPart.class == c) {
                    list.add(new ListFormDataBodyPartMultiPartInjectable(p.getSourceName()));
                } else if (FormDataContentDisposition.class == c) {
                    list.add(new ListFormDataContentDispositionMultiPartInjectable(p.getSourceName()));
                }
            } else if (FormDataBodyPart.class == p.getParameterClass()) {
                list.add(new FormDataBodyPartMultiPartInjectable(p.getSourceName()));
            } else if (FormDataContentDisposition.class == p.getParameterClass()) {
                list.add(new FormDataContentDispositionMultiPartInjectable(p.getSourceName()));
            } else {
                list.add(new FormDataMultiPartParamInjectable(p));
            }
        } else {
            Injectable injectable = getInjectableProviderContext().getInjectable(p, ComponentScope.PerRequest);
            list.add(injectable);
        }
    }
    return list;
}

So where it sees the parameter type is a List or Collection it will ignore it where the generic type is anything other than FormDataBodyPart or FormDataContentDisposition.

To get round the issue I have just changed my method to accept a comma delimited String for p3 in place of a List.

I came across another solution to this which may be better (and simpler) than having to manually process a comma separated version of the list etc. Posted belatedly to help others that find this post.

Change the multipart parameter from:

@FormDataParam("p3") List<String> p3,

to

@FormDataParam("p3") List<FormDataBodyPart> p3,

and then you have your list in P3 where you can get the parameter value for each FormDataBodyPart element using getValue().

Source: receiving-arrays-from-form-elements-with-jersey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!