Issue
I use Spring and JQuery for base project, I'm trying send list to server:
my data in front end (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
I send in url id
of parent Object too.
in spring:
@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;
}
}
and photo object is:
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;
my error is : Bad Request 400
Solution
- I change Jquery ajax: (
values
is an array from objects)
$.ajax({
url: http:\\...,
type: ...,
data: JSON.stringify( values ),
contentType: "application/json",
dataType: "json",
...
});
- I change my
FormDate
for server too:
[
{
title: t1
order: 100
mimeType: mt1
thumpnailMimeType: tmt1
height: 101
width: 103
byteSize: 200
thumpnailByteSize: 300
relPath: rp1
thumpnailRelPath: trp1
},
]
- add dependency
jackson
:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
- add
consumes
andproduces
to@RequestMapping
:
@RequestMapping(value = "/create/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Answered By - mehnet ali
Answer Checked By - Katrina (JavaFixing Volunteer)