将文件和JSON数据发布到Spring休息服务
  我正在构建一个用于上传文件的Spring rest服务。  有一个表单由多个字段和一个字段组成,用于上传文件。  在提交表单时,我发送一个多部分表单请求,即Content-Type作为multipart/form-data 。 
所以我尝试了下面
@RequestMapping(value = "/companies", method = RequestMethod.POST)
    public void createCompany(@RequestBody CompanyDTO companyDTO, @RequestParam(value = "image", required = false) MultipartFile image){
.................   
但是,上述不起作用。 所以暂时,我作为字符串发送JSON数据,并在休息服务中从该字符串形成公司对象
 @RequestMapping(value = "/companies", method = RequestMethod.POST)
        public void createCompany(@RequestParam("companyJson") String companyJson, @RequestParam(value = "image",required = false) MultipartFile image) throws JsonParseException, JsonMappingException, IOException{
            CompanyDTO companyDTO =  new ObjectMapper().readValue(companyJson, CompanyDTO.class);
.............................
无法使用@RequestBody发送JSON数据而不将JSON作为字符串传递?
使用@RequestParam将值附加到URL中。
@RequestParam注解对于复杂的JSON对象不起作用,它指定为整数或字符串。
如果它是一个Http POST方法,那么使用@RequestBody将使Spring将传入的请求映射到您创建的POJO(条件:如果POJO映射传入的JSON)
创建FormData()并追加你的json和文件
        if (form.validate()) {
        var file = $scope.file;
        var fd = new FormData();
        fd.append('jsondata', $scope.jsonData);  
        fd.append('file', file);
        MyService.submitFormWithFile('doc/store.html', fd, '', (response){
             console.log(response)
        });
    }
//上面调用的服务
    MyService.submitFormWithFile = function(url, data, config, callback) {
    $http({
        method : 'POST',
        url : url,
        headers : {
            'Content-Type' : undefined
        },
        data : data,
        transformRequest : function(data, headersGetterFunction) {
            return data;
        }
    }).success(function(response, status, header, config) {
        if (status === 200) {
            callback(response);
        } else {
            console.log("error")
        }
    }).error(function(response, status, header, config) {           
            console.log(response);
    });
};
//在使用ObjectMapper的java部分中
//it is like  string
fd.append('jsondata', JSON.stringify($scope.jsonData));
  @Autowired
  private ObjectMapper mapper;
 @RequestMapping(value = "/companies", method = RequestMethod.POST)
   public void createCompany(@RequestParam String jsondata,
        @RequestParam(required = true) MultipartFile file){
        CompanyDto companyDto=mapper.readValue(jsondata, CompanyDTO.class);
       ......
 }
                        链接地址: http://www.djcxy.com/p/48639.html
                        
                        
                    