簡體   English   中英

將多個參數傳遞給rest API - Spring

[英]Pass multiple parameters to rest API - Spring

我想弄清楚是否可以將JSON對象傳遞給其他API,或者將多個參數傳遞給該API? 以及如何在Spring中讀取這些參數? 讓我們假設網址如下所示:

例1 http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif

傳遞JSON對象是否有效,如下面的url?

例2 http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}

問題:

1)是否可以像往常一樣將JSON對象傳遞給url?

2)我們如何傳遞和解析Ex.1中的參數?

我試着寫一些方法來實現我的目標,但找不到合適的解決方案?

我試圖將JSON對象作為@RequestParam傳遞

http://localhost:8080/api/v1/mno/objectKey?id=1出現意外錯誤(type=Unsupported Media Type, status=415). Content type 'null' not supported (type=Unsupported Media Type, status=415). Content type 'null' not supported

http://localhost:8080/api/v1/mno/objectKey/id=1出現意外錯誤(type=Not Found, status=404). No message available (type=Not Found, status=404). No message available

http://localhost:8080/api/v1/mno/objectKey/%7B%22id%22:1%7D出現意外錯誤(type=Not Found, status=404). No message available (type=Not Found, status=404). No message available

@RequestMapping(value="mno/{objectKey}",
                method = RequestMethod.GET, 
                consumes="application/json")
    public List<Book> getBook4(@RequestParam ObjectKey objectKey) {
        ...
    }

我試圖將JSON對象作為@PathVariable傳遞

@RequestMapping(value="ghi/{objectKey}",method = RequestMethod.GET)
    public List<Book> getBook2(@PathVariable ObjectKey objectKey) {
        ...         
    }

我創建了這個對象來保存id參數和其他參數,如名稱等....

class ObjectKey{
        long id;
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
    }

(1)是否可以像在Ex.2中一樣將JSON對象傳遞給url?

不,因為http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}不是有效的URL。

如果你想以RESTful方式做,請使用http://localhost:8080/api/v1/mno/objectKey/1/Saif ,並定義你的方法如下:

@RequestMapping(path = "/mno/objectKey/{id}/{name}", method = RequestMethod.GET)
public Book getBook(@PathVariable int id, @PathVariable String name) {
    // code here
}

(2)我們如何傳遞和解析Ex.1中的參數?

只需添加兩個請求參數,並提供正確的路徑。

@RequestMapping(path = "/mno/objectKey", method = RequestMethod.GET)
public Book getBook(@RequestParam int id, @RequestParam String name) {
    // code here
}

更新 (來自評論)

如果我們有一個復雜的參數結構怎么辦?

 "A": [ { "B": 37181, "timestamp": 1160100436, "categories": [ { "categoryID": 2653, "timestamp": 1158555774 }, { "categoryID": 4453, "timestamp": 1158555774 } ] } ] 

將其作為POST發送,其中包含請求正文中的JSON數據,而不是URL,並指定application/json的內容類型。

@RequestMapping(path = "/mno/objectKey", method = RequestMethod.POST, consumes = "application/json")
public Book getBook(@RequestBody ObjectKey objectKey) {
    // code here
}

你可以在url中傳遞多個參數

HTTP://本地主機:2000 /定制品牌=戴爾和限制= 20&價格= 20000&排序= ASC

並且為了獲得此查詢字段,您可以使用map like

    @RequestMapping(method = RequestMethod.GET, value = "/custom")
    public String controllerMethod(@RequestParam Map<String, String> customQuery) {

        System.out.println("customQuery = brand " + customQuery.containsKey("brand"));
        System.out.println("customQuery = limit " + customQuery.containsKey("limit"));
        System.out.println("customQuery = price " + customQuery.containsKey("price"));
        System.out.println("customQuery = other " + customQuery.containsKey("other"));
        System.out.println("customQuery = sort " + customQuery.containsKey("sort"));

        return customQuery.toString();
    }

是的,可以在URL中傳遞JSON對象

queryString = "{\"left\":\"" + params.get("left") + "}";
 httpRestTemplate.exchange(
                    Endpoint + "/A/B?query={queryString}",
                    HttpMethod.GET, entity, z.class, queryString);

可以給出多個參數,如下所示,

    @RequestMapping(value = "/mno/{objectKey}", method = RequestMethod.GET, produces = "application/json")
    public List<String> getBook(HttpServletRequest httpServletRequest, @PathVariable(name = "objectKey") String objectKey
      , @RequestParam(value = "id", defaultValue = "false")String id,@RequestParam(value = "name", defaultValue = "false") String name) throws Exception {
               //logic
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM