我正在尝试像下面这样传递Json数据
for (var t = 0; t < files.length; t ++ ){
contents.push({"name":files[t].name,"selected":true,"url":files[t].url});
}
控制器
@RequestMapping(value = "/ajax/candidate/onboarding/multidownload",method = RequestMethod.POST,consumes = "application/json")
public @ResponseBody RequestTTT downloadCandidateDoc(HttpServletResponse res,@RequestBody List<DownloadItem> files) {
requestttt.setFiles(files);
return requestttt;
}
Class RequestTTT
class RequestTTT {
Long candidateId;
List<DownloadItem> files;
}
DownloadItem类
public class DownloadItem {
private String name;
private boolean selected;
}
我得到的结果是:
400错误的功能请求
$.ajax({
type : "POST",
url : "${contextPath}/ajax/candidate/onboarding/multidownload",
data : JSON.stringify(data),
dataType: 'json',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
cache: false,
timeout: 100000,
success: function (data) {
console.log("SUCCESS : ", data);
},
error: function (e) {
console.log("ERROR : ", e);
}
})
答案 0 :(得分:0)
抱歉,迟到了。
请求列表项类别:
public class DownloadItem {
private String name;
private String selected;
private String url;
/*{
"name": "logo.png",
"selected": true,
"url": "192.168.84.30/static/candidate/india/1/120170623163651logo.png"
}*/
// Getter
// Setter
}
响应类别:
import java.util.List;
public class Response {
private Long candidateId;
private List<DownloadItem> files;
// Getter
// Setter
}
您的控制器代码将在下面:
@PostMapping("ajax/candidate/onboarding/multidownload")
public ResponseEntity<Response> downloadCandidateDoc(@RequestBody List<DownloadItem> request) {
Response response=new Response();
response.setCandidateId(11L);
response.setFiles(request);
return new ResponseEntity<>(response, HttpStatus.OK);
}
这里是进口:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
修改:
我在邮递员中对此进行了检查。这是样本输入输出。 http://IP:PORT/YOUR_CONTEXT_PATH/ajax/candidate/onboarding/multidownload
请求Json
[
{
"name": "logo.png",
"selected": true,
"url": "192.168.84.30/static/candidate/india/1/120170623163651logo.png"
},
{
"name": "Chrysanthemum.jpg",
"selected": true,
"url": "192.168.84.30/static/candidate/india/1/…"
}
]
响应:
{
"candidateId": 11,
"files": [
{
"name": "logo.png",
"selected": "true",
"url": "192.168.84.30/static/candidate/india/1/120170623163651logo.png"
},
{
"name": "Chrysanthemum.jpg",
"selected": "true",
"url": "192.168.84.30/static/candidate/india/1/…"
}
]
}