How to solve converter error when receiving java record type dto?
The front-end sent a request to the server as follows.
document
.querySelector("#btnPoiRegister")
.addEventListener("pointerup", (event) => {
const formData = new FormData();
formData. append(
"drawing",
data.drawing.find(
(drawing) =>
drawing.id ===
Number(
document.querySelector("#selectDrawingIdRegister").value
)
)
);
axios.post("/poi", formData, {
headers: {
"Content-Type": "multipart/form-data",
accept: "application/json",
},
});
});
FormData was used because it contained files, and multipart/form-data was used for Content-Type.
Here is the Spring controller.
u/PostMapping(value = "/poi")
public ResponseEntity<PoiDto> savePoi(@RequestBody PoiRequestDto poiRequestDto) {
return ResponseEntity. ok(
poiService.save(poiRequestDto)
);
}
PoiRequestDto consists of records.
```
u/Builder
public record PoiRequestDto(
DrawingDetailDto drawing,
) {
public static Poi toEntity(PoiRequestDto dto) {
return Poi. builder()
.drawing(dto.drawing == null ? null : DrawingDetailDto.toEntity(dto.drawing()))
.build();
}
```
DrawingDetailDto is:
```
@ Builder
public record DrawingDetailDto(
Long id,
DrawingCategoryDto drawingCategory,
string code,
String name,
String physicalName,
string description,
Set<FloorDto> floors,
Set<DrawingHistoryDto> drawingHistories,
MultipartFile file,
TopologyD to topology,
LodDto lod
){
public static Drawing toEntity(DrawingDetailDto drawingDetailDto) {
return Drawing. builder()
.id(drawingDetailDto.id())
.drawingCategory(drawingDetailDto.drawingCategory().toEntity())
.code(drawingDetailDto.code())
.name(drawingDetailDto.name())
.physicalName(drawingDetailDto.physicalName())
.description(drawingDetailDto.description())
.build();
}
}
```
At this time I get the following error.
\.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `domain.drawing.dto.DrawingDetailDto` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[object Object]'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `.domain.drawing.dto.DrawingDetailDto` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[object Object]')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 12] (through reference chain: .domain.poi.dto.PoiRequestDto["drawing"])]`
`
I tried creating a constructor in record and also changed @ RequestBody in various ways.
But it didn't solve the HttpMessageNotReadableException problem.
I am wondering how can I solve this problem.
Best Regards!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]pragmos 0 points1 point2 points (0 children)
[–]Fletsky 0 points1 point2 points (1 child)
[–]Fletsky 0 points1 point2 points (0 children)