Spring MVC 向rest API传递多个参数- Spring

tzcvj98z  于 2023-08-06  发布在  Spring
关注(0)|答案(4)|浏览(165)

我试图弄清楚是否可以将JSON对象传递给REST API,或者将多个参数传递给该API?如何在Spring中读取这些参数?假设URL看起来像下面的例子:
Ex.1 http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif
是否可以像下面的url那样传递JSON对象?
例2 http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}
问题:
1)是否可以像Ex.2中那样将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
http://localhost:8080/api/v1/mno/objectKey/id=1出现意外错误(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

@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参数和其他参数,如name等。

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

jhdbpxl9

jhdbpxl91#

(1)是否可以像示例2中那样将JSON对象传递给url?
不可以,因为http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}不是有效的URL。
如果您想以REST风格的方式执行此操作,请使用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发送,并在请求正文(而不是URL)中使用JSON数据,然后指定内容类型application/json

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

ar5n3qh5

ar5n3qh52#

你可以像这样在url中传递多个参数
http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc
为了得到这个查询字段,你可以使用map

@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();
    }

字符串

yzuktlbb

yzuktlbb3#

多个参数可以如下给出,

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

字符串

zyfwsgd6

zyfwsgd64#

可以在URL中传递JSON对象

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

字符串

相关问题