Java 将多个参数传递给RESTAPI-Spring
我试图弄清楚是否可以将JSON对象传递给RESTAPI,或者将多个参数传递给该API?如何在Spring中读取这些参数?让我们假设url类似于以下示例: 例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传递Java 将多个参数传递给RESTAPI-Spring,java,json,spring,rest,spring-mvc,Java,Json,Spring,Rest,Spring Mvc,我试图弄清楚是否可以将JSON对象传递给RESTAPI,或者将多个参数传递给该API?如何在Spring中读取这些参数?让我们假设url类似于以下示例: 例1http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif 像下面的url一样传递JSON对象有效吗 例2http://localhost:8080/api/v1/mno/objectKey/{“id”:1,“name”:“Saif”} 问题: 1) 是否可以像Ex.2那样将JSON对
http://localhost:8080/api/v1/mno/objectKey?id=1
出现意外错误(类型=不支持的媒体类型,状态=415)。不支持内容类型“null”
http://localhost:8080/api/v1/mno/objectKey/id=1
出现意外错误(类型=未找到,状态=404)。没有可用的消息
@RequestMapping(value="mno/{objectKey}",
method = RequestMethod.GET,
consumes="application/json")
public List<Book> getBook4(@RequestParam ObjectKey objectKey) {
...
}
http://localhost:8080/api/v1/mno/objectKey/%7B%22id%22:1%7D
出现意外错误(类型=未找到,状态=404)。没有可用的消息
@RequestMapping(value="mno/{objectKey}",
method = RequestMethod.GET,
consumes="application/json")
public List<Book> getBook4(@RequestParam ObjectKey objectKey) {
...
}
(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中传递JSON对象
queryString = "{\"left\":\"" + params.get("left") + "}";
httpRestTemplate.exchange(
Endpoint + "/A/B?query={queryString}",
HttpMethod.GET, entity, z.class, queryString);
您可以在url中传递多个参数,如 为了得到这个查询字段,您可以使用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();
}
@RequestMapping(method=RequestMethod.GET,value=“/custom”)
公共字符串控制器方法(@RequestParam-Map-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”));
返回customQuery.toString();
}
可以如下所示给出多个参数
@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
}
@RequestMapping(value=“/mno/{objectKey}”,method=RequestMethod.GET,products=“application/json”)
公共列表getBook(HttpServletRequest HttpServletRequest,@PathVariable(name=“objectKey”)字符串objectKey
,@RequestParam(value=“id”,defaultValue=“false”)字符串id,@RequestParam(value=“name”,defaultValue=“false”)字符串名称)引发异常{
//逻辑
}
注释和将对象作为主体传递怎么样?有什么特别的原因让您这样做吗?您应该能够从与实体匹配的请求中获取对象。很抱歉,如果示例不准确,这只是一个概念证明,我希望能够根据搜索条件从数据库中获取建议项的列表。我将有一个小部件来调用RESTAPI并传递参数。但我现在正努力关注后端部分谢谢@Andreas,它可以工作:)但我还有一些问题吗?如果我们有一个复杂的参数结构怎么办?**“A”:[{“B”:37181,“时间戳”:1160100436,“类别”:[{“类别ID”:2653,“时间戳”:1158555774},{“类别ID”:4453,“timestamp”:1158555774}]}]**如果在GET
中不发送类似的内容,则使用POST
,并在有效负载中包含JSON,指定内容类型为application/JSON
。感谢Andreas的上次更新,是否可以使用JavaScript创建帖子,因为我不想使用表单使用RESTAPI,我希望JavaScript使用它。是的。我建议你做一个网络搜索。我还建议使用一个库,比如jQuery
,以使它更简单。@jolumg有些人没有读到:“POST方法请求目标资源根据资源自身的特定语义处理请求中包含的表示”。当然,最常见的语义是资源创建一些东西,但流程更通用,可能意味着其他事情,例如使用提供的数据运行信用检查GET
将是一种不合适的方法。这确实帮助我创建了一个带有动态URL参数的GET服务。谢谢@Om Sharma
@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
}