Home > other >  Get form data from a different route in Spring PostMapping
Get form data from a different route in Spring PostMapping

Time:12-09

I have an API controller:

@RestController
@RequestMapping("api")
public class ListingRestController {
    @PostMapping("/listings/edit/{id}")
    public void editListing(ListingForm newListing, @PathVariable Integer id, Model model) {
        ListingDto newListingDto = new ListingDto(newListing.getId(), newListing.getUserId(), newListing.getTitle());
        model.addAttribute("submitURL", String.format("edit/%s", id));
        listingService.deleteListingById(id);
        listingService.addListing(newListingDto);
    }
}

It takes a POST request from api/listings/edit/{id}. Also it should get the data from form inputs ListingForm newListing.

The problem is, the form is defined in a route listings/edit/{id}, so the controller method cannot get data. Any idea how I can get form data from route listings/edit/{id} in my API controller?

CodePudding user response:

You must define GET api beside Api Post to get data. First, client call API GET to get data from server and place data to the form. After finish editing data, call API POST to post data to server.

CodePudding user response:

You can write @RequestBody annotation to ListeningForm parameter

  • Related