Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - two different POST methods with the same URL but different produced content type

I have the following controller:

@RequiredArgsConstructor
@RestController
public class OwnerViewController implements ApiOwnerViewController {

    private static final String TEXT_CSV = "text/csv";

    private final PrivateOwnerFacade privateOwnerFacade;

    @PostMapping("/boat/v1/private-owners/search")
    public OwnerViewResponse searchOwners(@Valid @RequestBody SearchOwnersRequest request,
                                          Pageable pageable) {
        return privateOwnerFacade.findOwners(request, pageable);
    }

    @PostMapping(value = "/boat/v1/private-owners/search", produces = TEXT_CSV)
    public ResponseEntity<Resource> exportToCsv(@Valid @RequestBody SearchOwnersRequest request, Pageable pageable)
            throws IOException {

So I have two methods mapped to the same url and that both accept POST request BUT produce different content type - the first one produces application/json while the second produces text/csv.

Then, when I'm trying to make a request and set header Accept: text/csv I get 406 from the server.

I wonder if it is really possible to do such things with spring? Or is the only way to change second method so that it accepts GET requests?

Thanks

like image 458
Andrey Yaskulsky Avatar asked Oct 19 '25 12:10

Andrey Yaskulsky


1 Answers

@RequiredArgsConstructor
@RestController
public class SearchController {

    @PostMapping(value = "/search", produces = {APPLICATION_JSON})
    public SearchResponse search(@Valid @RequestBody SearchRequest request,
                                          Pageable pageable) {
    }

    @PostMapping(value = "/search", produces = {TEXT_CSV})
    public ResponseEntity<Resource> export(@Valid @RequestBody SearchRequest request, Pageable pageable) throws IOException {

    }


}
like image 133
Andrey Yaskulsky Avatar answered Oct 22 '25 01:10

Andrey Yaskulsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!