我一直在这里寻找解决方案,但没有发现对我的情况有用的东西。
我的 Dao 需要一个 String[]
和一个 String
,所以我这样做了:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody String[] isbn,String username) {
rentService.newRent(isbn, username);
}
现在,我尝试从 Postman 调用映射链接执行 POST
,但我一直收到不允许的方法 (405
)。
我尝试了很多,这看起来是最好的方法,但仍然不起作用。
[
{ { "isbn":"123"},{"isbn":"1234"},
{ "username" : "zappa"}
]
或
{
"isbn": ["123", "1234"],
"username": "zappa"
}
我错过了什么吗?想不通!
最佳答案
您必须创建一个新实体Rent
public class Rent{public string[] isbn; public string username;}
然后你将方法更改为:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody Rent rentRequest) {
rentService.newRent(rentRequest.isbn, rentRequest.username);
}
关于java - 从其余 Controller 方法请求 String [] + String 作为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990211/