Spring - 两种不同的 POST 方法具有相同的 URL 但产生的内容类型不同 [英] Spring - two different POST methods with the same URL but different produced content type

查看:22
本文介绍了Spring - 两种不同的 POST 方法具有相同的 URL 但产生的内容类型不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器:

@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 {

所以我有两个方法映射到同一个 url 并且都接受 POST 请求但产生不同的内容类型 - 第一个产生 application/json 而第二个产生 text/csv.

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.

然后,当我尝试发出请求并设置标头时Accept: text/csv 我从服务器得到 406.

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

不知道用spring真的可以做这样的事情吗?或者是更改第二种方法以使其接受 GET 请求的唯一方法?

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?

谢谢

推荐答案

@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 {

    }


}

这篇关于Spring - 两种不同的 POST 方法具有相同的 URL 但产生的内容类型不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆