我有一个服务方法,我将人员加入团队:
@Transactional
public void addPersonsToTeams(Long teamId, Long personId) {
Assert.notNull(personId, "Object can't be null!");
Assert.notNull(teamId, "Object can't be null!");
try {
Person person = personRepository.getOne(personId);
Team team = teamRepository.getOne(teamId);
person.getTeams().add(team);
personRepository.save(person);
} catch (Exception e) {
throw new CreateEntityException();
}
}
现在在我的休息控制器类中,我想创建一个post方法来在postman中测试它。但这是我第一次用两个参数方法,我不知道如何创建它。 其实我只有这个:
@PostMapping("/addPeopleToTeams")
public ResponseEntity<?> addPeopleToTeam(@RequestBody Long teamId, Long personId){
}
这是好方法吗?也许有人有一些问题?
答案 0 :(得分:0)
您可以将整个帖子正文转换为POJO。以下是类似的东西
@RequestMapping(value = { "/api/pojo/edit" }, method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public Boolean addPeopleToTeam( @RequestBody Pojo pojo) { return false; }
Pojo中的每个字段(包括getter / setter)应该与控制器接收的Json请求对象匹配。
答案 1 :(得分:0)
可能将
path = @"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"""";
作为
path = $@"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u {txtBoxDBUsername.Text} -p {txtBoxDBName.Text} > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup{maskeTxtBoxDBFile.Text}""";
。这样,您就会在teamId
中使用@PathVariable
致电/addPeopleToTeams/{teamId}
。或者,你也可以发送一个像这样形成的json:
personId
然后,您可以使用@RequestBody
在{
"personId" : "xx",
"teamId" : "xx"
}
中使用此对象进行调用。你可以做很多不同的方式。只需按照它在其余代码中最有意义的方式进行操作。
答案 2 :(得分:0)
@RequestMapping(value = { "/addPeopleToTeams/{teamId}/{personId}" }, method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public Boolean addPeopleToTeam( @PathVariable("teamId") Long teamId, @PathVariable("personId") Long personId)
但是您需要像/addPeopleToTeams/1/2