简体   繁体   English

使用spring-mvc 4.1解析多部分/表单数据(不上传文件)时遇到问题

[英]trouble parsing multipart/form-data (not file upload) with spring-mvc 4.1

I need to parse a multipart request in a spring-mvc controller. 我需要在spring-mvc控制器中解析多部分请求。 Spring version is 4.1.6. 春季版本为4.1.6。 Example of the request: 请求示例:

HEADER host: "localhost:8080"
HEADER accept: "*/*"
HEADER content-length: "539"
HEADER expect: "100-continue"
HEADER content-type: "multipart/form-data; boundary=------------------------2e2bf5fa2f7bbfbf"
BODY:
--------------------------2e2bf5fa2f7bbfbf
Content-Disposition: form-data; name="objectid"

160714.110239.GG
--------------------------2e2bf5fa2f7bbfbf--
Content-Disposition: form-data; name="geojson"

[a very long json string]
--------------------------2e2bf5fa2f7bbfbf

The exact formatting of the request is not under my control. 请求的确切格式不受我的控制。

As long as I use the whole @RequestBody, my RequestMapping is resolved: 只要我使用整个@RequestBody,我的RequestMapping就可以解决:

@RequestMapping(value = "/coordinates", method = RequestMethod.POST)
public @ResponseStatus(value = HttpStatus.OK)
void coordinates(final HttpServletRequest request,
                 final @RequestBody String body) {
  final Enumeration<String> headerNames = request.getHeaderNames();
  while (headerNames.hasMoreElements()) {
    final String element = headerNames.nextElement();
    System.out.println("HEADER " + element + ": \"" + request.getHeader(element) + "\"" );
  }
  System.out.println("BODY:");
  System.out.println(body);
}

But when I change the signature in order to get the parts directly, the client gets an error message, saying "Required String parameter 'objectid' is not present", and my mapping is not resolved anymore (tested with a breakpoint). 但是,当我更改签名以直接获取零件时,客户端会收到一条错误消息,提示“不存在必需的String参数'objectid'”,并且我的映射也不再解析(已使用断点进行了测试)。 This is the failing code: 这是失败的代码:

@RequestMapping(value = "/coordinates", method = RequestMethod.POST)
public @ResponseStatus(value = HttpStatus.OK)
void coordinates(final HttpServletRequest request,
                 final @RequestParam("objectid") String objectid,
                 final @RequestParam("geojson") String geojson) {
  final String remoteAddr = request.getRemoteAddr();
  System.out.println("coordinates from " + remoteAddr
      + ":\nobjectid=" + objectid
      + "\ngeojson=" + geojson);
}

What am I doing wrong ? 我究竟做错了什么 ?

Since you are submitting the data using POST method,multi-part content will go in the request body not as request parameters. 由于您是使用POST方法提交数据的,因此多部分内容将作为请求参数而不是进入请求主体。

In second case, you annotated with request param object, spring will check for a parameter in the request url. 在第二种情况下,您使用请求参数对象进行了注释,spring将检查请求url中的参数。 If its not available spring will throw an error. 如果其不可用,弹簧将引发错误。 Additionally you can make the request parameter as optional or you can give default value 此外,您可以将request参数设为可选参数,也可以提供默认值

Resolved. 解决。

There was one thing missing in my spring configuration: 我的弹簧配置中缺少一件事:

<bean id="multipartResolver"
      class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
  <property name="maxUploadSize" value="100000"/>
</bean>

And with that resolver, the @RequestParam annotation (as in my second java source example) works. 使用该解析器,@ RequestParam批注(如我的第二个Java源示例所示)可以工作。 Yes, it's @RequestParam although the values are not part of the URL. 是的,它是@RequestParam,尽管这些值不是URL的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM