正确写法
@RequestMapping(value = "/test/{name}", method = RequestMethod.POST)
@ApiOperation(value = "shape文件解析", notes = "shape文件解析")
@ApiOperationSupport(order = 31)
public HttpResponse<Boolean> test(@RequestParam(value = "file") MultipartFile file, @RequestPart(value = "body") JSONObject body, @PathVariable String name) throws Exception {
return new HttpResponse<>(true);
}
接口注意事项
文件参数使用@RequestParam注解。Json 参数使用@RequestPart注解 @RequestParam和@RequestPart的区别是:@RequestParam适用于name-valueString类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML) Json对象只能是Alibaba的fastjson对象(JsonObject)
PostMan测试注意事项
Headers中的Content-Type为PostMan自动生成 form-data中的Content-Type为手动设置,默认不会展示,需要点击右上角的三个点,json参数需要设置,文件参数不用,json参数格式如下 { “gid”: “gid”, “code”: “代码”, “name”: “name”, “clasid”: “clasid”, “the_geom”: “the_geom”, “elemstime”: “elemstime”, “type”: “类型” }
我选择的参数是文件(file)和字符串(body),“name”直接放在接口地址中
@RequestMapping(value = "/adddata/{name}", method = RequestMethod.POST)
@ApiOperation(value = "shape文件解析", notes = "shape文件解析")
@ApiOperationSupport(order = 31)
public HttpResponse<Boolean> addData(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "body") String body, @PathVariable String name) throws Exception {
return new HttpResponse<>(StructureService.addData(file,body,name));
}
一开始要求的是文件和json字符串,但是尝试了很多次都不行,不管是在Postman中将Content-Type改为“multipart/form-data”还是不设置Content-Type都没用,所以还是选择了上面代码的形式,然后在service中将String转化为JsonObject,再强转Map
Map jsonMap = (Map) JSONArray.parseObject(body);
|