将多个参数传递给

将多个参数传递给

本文介绍了将多个参数传递给rest API - Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚是否可以将JSON对象传递给其他API,或者将多个参数传递给该API?以及如何在Spring中读取这些参数?让我们假设网址如下所示:

I am trying to figure out if it is possible to pass a JSON object to rest API, Or pass a multiple parameters to that API ? And how to read these parameters in Spring ? Lets assume that the url looks like the below examples :

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

传递JSON对象是否有效,如下面的url?

Is it valid to pass a JSON object like in the url below ?

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

问题:

1)是否可以将JSON对象传递给如Ex.2中的url?

1) Is it possible to pass a JSON object to the url like in Ex.2?

2)我们如何传递和解析Ex.1中的参数?

2) How can we pass and parse the parameters in Ex.1?

我试着写一些方法来实现我的目标,但找不到合适的解决方案?

I tried to write some methods to achieve my goal, but could not find the right solution?

I试图将JSON对象传递为@RequestParam

I tried to pass JSON object as @RequestParam

http:// localhost:8080 / api / v1 / mno / objectKey?id = 1 出现意外错误(type = Unsupported Media Type,status = 415)。内容类型'null'不受支持

http:// localhost:8080 / api / v1 / mno / objectKey / id = 1 出现意外错误(type = Not Found,status = 404)。没有可用消息

http:// localhost:8080 / api / v1 / mno / objectKey /%7B% 22id%22:1%7D 出现意外错误(type = Not Found,status = 404)。没有可用消息

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

I试图将JSON对象传递为@PathVariable

I tried to pass the JSON object as @PathVariable

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

I创建此对象以保存id参数和其他参数,如名称等....

I created this object to hold the id parameter and other parameters like name , etc ....

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


推荐答案

否,因为 http:// localhost:8080 / api / v1 / mno / objectKey / {id:1,name:Saif} 无效URL。

如果您想以RESTful方式执行此操作,请使用 http:// localhost:8080 / api / v1 / mno / objectKey / 1 / Saif ,并定义了这样的方法:

If you want to do it the RESTful way, use http://localhost:8080/api/v1/mno/objectKey/1/Saif, and defined your method like this:

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



只需添加两个请求参数,并给出正确的路径。

Just add two request parameters, and give the correct path.

@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
    } ]
} ]


使用请求正文中的JSON数据将其作为 POST 发送,而不是在URL中发送,并指定内容类型 application / json

Send that as a POST with the JSON data in the request body, not in the URL, and specify a content type of application/json.

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

这篇关于将多个参数传递给rest API - Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:15