前端javascript发起post请求java端提供服务参数分别为ListA和bizIDA包含fileid和filename属性请分别给出两端代码
前端javascript发起post请求:
const data = {
list: [
{ fileid: 1, filename: 'file1' },
{ fileid: 2, filename: 'file2' },
{ fileid: 3, filename: 'file3' },
],
bizID: '123456',
};
fetch('/api/upload', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
Java端提供服务:
@PostMapping("/upload")
public void upload(@RequestBody Map<String, Object> data) {
List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
String bizID = (String) data.get("bizID");
List<A> aList = new ArrayList<>();
for (Map<String, Object> item : list) {
A a = new A();
a.setFileid((Integer) item.get("fileid"));
a.setFilename((String) item.get("filename"));
aList.add(a);
}
// TODO: 处理业务逻辑
}
``
原文地址: https://www.cveoy.top/t/topic/cSqB 著作权归作者所有。请勿转载和采集!