Spring / Jboss - 当前请求不是多部分请求

时间:2017-02-09 08:10:31

标签: java spring spring-mvc tomcat jboss

我正在尝试创建一个rest webservice,用户使用一些参数发送httpPostrequest。这是我的代码

@RequestMapping(value="/SubmitDetails")
@ResponseBody
public String saveCase(@RequestParam("details") String details){
       //Do SomeWork
     return "some response";
}

但是当我从浏览器访问此URL时,它会抛出:

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

root cause

org.springframework.web.multipart.MultipartException: Current request is not a multipart request
    org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:188)
    org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

此代码适用于Tomcat,但不适用于JBOSS 4.2

对于发布请求我正在使用Android的Volly:

void HttpStringRequest(final int request , String url, final Map<String,String> keyVal){

    StringRequest stringRequest = new StringRequest(request, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Helper.getmHelper().v(this.getClass().getName()+":RESPONSE:",response);

            hideProgress();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Helper.getmHelper().v(this.getClass().getName()+":ERROR:","Error");

            hideProgress();
        }
    }
    ) {
        @Override
        protected Map<String, String> getParams() {
            Iterator<Map.Entry<String, String>> it = keyVal.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
                Helper.getmHelper().v(pairs.getKey(), pairs.getValue());
            }
            return keyVal;
        }

       @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "text/plain; charset=utf-8");
            return headers;
        }


    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(60*1000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);
}

1 个答案:

答案 0 :(得分:0)

检查是否在Spring-Servlet.xml中添加了CommonsMultipartResolver。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

然后,将enctype添加到表单中的multipart / form-data:

<form id="fileupload" method="post" enctype="multipart/form-data">

最后在Controller,Request&gt; MultipartHttpServletRequest:

@RequestMapping(value="/SubmitDetails"  method = RequestMethod.POST)
public String saveCase(MultipartHttpServletRequest request) {}