java 将文件和 JSON 数据发布到 Spring 休息服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33729591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting a file and JSON data to Spring rest service
提问by Anand
I am building a Spring rest service for uploading a file. There is a form that consists of various field and one field for uploading a file. On submitting that form, I am sending a multipart form request i.e. Content-Type
as multipart/form-data
.
我正在构建一个用于上传文件的 Spring 休息服务。有一个表单,由多个字段和一个用于上传文件的字段组成。在提交该表单时,我发送了一个多部分表单请求,即Content-Type
作为multipart/form-data
.
So I tried with below
所以我尝试在下面
@RequestMapping(value = "/companies", method = RequestMethod.POST)
public void createCompany(@RequestBody CompanyDTO companyDTO, @RequestParam(value = "image", required = false) MultipartFile image){
.................
But, the above didn't work. So for time being,i sent JSON data as String and forming Company Object from that String in rest service like
但是,上面的方法没有用。因此,暂时,我将 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);
.............................
Can't I send JSON data with @RequestBody without passing JSON as String?
我不能在不将 JSON 作为字符串传递的情况下使用 @RequestBody 发送 JSON 数据吗?
回答by Anand
Appending the values to the URL what u have been doing now using @RequestParam.
将值附加到您现在使用@RequestParam 所做的 URL 中。
@RequestParam annotation will not work for complex JSON Objects , it is specifi for Integer or String .
@RequestParam 注释不适用于复杂的 JSON Objects,它是针对 Integer 或 String 的。
If it is a Http POST method , use of @RequestBody will make the Spring to map the incoming request to the POJO what u have created (condition: if the POJO maps the incoming JSON)
如果是 Http POST 方法,则使用 @RequestBody 将使 Spring 将传入的请求映射到您创建的 POJO(条件:如果 POJO 映射传入的 JSON)
回答by Kalaiselvan
create FormData() and append your json and file
创建 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)
});
}
//Service called in above
//上面调用的服务
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);
});
};
// in your java part using ObjectMapper
// 在你的 java 部分使用 ObjectMapper
//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);
......
}
回答by Ankit Adlakha
Use below code snippet:
使用以下代码片段:
@RequestMapping(value= "/path", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseObject methodName(MyData input, @RequestParam(required=false) MultipartFile file) {
// To Do
}