Home > Enterprise >  rest api - PUT method does not consume the data that GET method fetches
rest api - PUT method does not consume the data that GET method fetches

Time:09-16

I obtain data with GET method and try to fed them to PUT method. And I get a bad request error. But when I edit the JSON as below then everithing works fine. So why does the first JSON not work?

Controller class:

    @RestController
    @RequestMapping("/api")
    public class FileController {
    
        private final FileService fileService;
    
        private final DocService docService;
    
        private final DraftFileToPresentationConverter draftFileToPresentationConverter;
    
        @Autowired
        private DocFileRelationRepository docFileRelationRepository;
    
        @Autowired
        public FileController(FileService fileService,
                              DocService docService,
                              DraftFileToPresentationConverter draftFileToPresentationConverter) {
            this.fileService = fileService;
            this.docService = docService;
            this.draftFileToPresentationConverter = draftFileToPresentationConverter;
        }
    
        @GetMapping("/docs/files/{id}")
        public ResponseEntity<FilePresentation> getDraftFile(@PathVariable Long id) {
            DraftFile file = fileService.getFile(id);
            FilePresentation filePresentation = draftFileToPresentationConverter.convert(file);
            return new ResponseEntity<>(filePresentation, HttpStatus.OK);
        }
    
        @PutMapping("/docs/files/{id}")
        public ResponseEntity<FilePresentation> updateDraftFile(@RequestBody FilePresentation filePresentation) {
            fileService.update(draftFileToPresentationConverter.convert(filePresentation));
            return new ResponseEntity<>(filePresentation, HttpStatus.OK);
        }

DTO:

    @Data
    public class FilePresentation {
    
        private Long id;
    
        private States state;
    
        private String name;
    
        private String user;
    
        private Date dateUpload;
    
        private Long lenght;
    
        private IdRef document;
    
        private IdRef judgeDoc;
    
        public String getSize()
        {
            Double result = Double.valueOf(lenght/1024.0/1024.0);
    
            if(result<1)
            {
                result =  Double.valueOf(lenght/1024.0);
    
                if(result<1)
                {
                    return (lenght   " байт");
                }
    
                return (result.intValue()   " Кбайт");
            }
                return (result.intValue()   " Мбайт");
        }
    
    }

Troublesome class:

@Data
public class IdRef {

    public IdRef(Long id) {
        this.id = id;
    }

    private Long id;

}

JSON that I get with GET method and try to fed to PUT method (and get 400 Bad Request):

{
    "id": 21,
    "state": "DRAFT",
    "name": "DNS-list.tiff",
    "user": "JSmith",
    "dateUpload": null,
    "lenght": 28,
    "document": {
        "id": 141
    },
    "judgeDoc": null,
    "size": "28 байт"
}

JSON that DOES work

{
    "id": 21,
    "state": "DRAFT",
    "name": "DNS-list.tiff",
    "user": "JSmith",
    "dateUpload": null,
    "lenght": 28,
    "document": 141,
    "judgeDoc": null,
    "size": "28 байт"
}

CodePudding user response:

Try to update RequestBody with @PathVariable

CodePudding user response:

The constructor in IdRef was the reason. I removed the constructor and it works fine now, my controller consumes the first JSON without errors.

  • Related