我將 Spring 和 JQuery 用于基礎專案,我正在嘗試將串列發送到服務器:
我在前端的資料(FormData):
photos[0].title: t1
photos[0].order: 100
photos[0].mimeType: mt1
photos[0].thumpnailMimeType: tmt1
photos[0].height: 101
photos[0].width: 103
photos[0].byteSize: 200
photos[0].thumpnailByteSize: 300
photos[0].relPath: rp1
photos[0].thumpnailRelPath: trp1
我也發送了父物件的 url id。
在春天:
@RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
public String addPhoto(@PathVariable Long id, @RequestBody List<PhotoDto> photos, HttpServletRequest req, HttpServletResponse response) {
try {
GalleryDto gallery = galleryApplicationService.get(id);
// ...
return "ok";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
照片物件是:
private String title;
private Integer order;
private String mimeType;
private String thumpnailMimeType;
private Integer height;
private Integer width;
private Long byteSize;
private Long thumpnailByteSize;
private String relPath;
private String thumpnailRelPath;
private GalleryDto gallery;
我的錯誤是:Bad Request 400
uj5u.com熱心網友回復:
- 我改變了 Jquery ajax:(
values是一個物件陣列)
$.ajax({
url: http:\\...,
type: ...,
data: JSON.stringify( values ),
contentType: "application/json",
dataType: "json",
...
});
- 我也改變了我
FormDate的服務器:
[
{
title: t1
order: 100
mimeType: mt1
thumpnailMimeType: tmt1
height: 101
width: 103
byteSize: 200
thumpnailByteSize: 300
relPath: rp1
thumpnailRelPath: trp1
},
]
- 添加依賴
jackson:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
- 添加
consumes和:produces_@RequestMapping
@RequestMapping(value = "/create/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452552.html
標籤:列表 弹簧MVC http-status-code-400
