[英]Junit passing multiple parameters to rest service
I have a rest controller like bellow : 我有一个像bellow这样的休息控制器:
@RequestMapping(value = "/create", method = RequestMethod.POST)
public
@ResponseBody
GlobalResponse createDeal(@RequestBody Deal deal,@RequestBody Owner owner) {
// code here
}
I use Junit and Mockito for my test : 我使用Junit和Mockito进行测试:
@Test
public void createDeal() throws Exception{
this.mockMvc.perform(post("/v1/Deal/create").content("\"deal\":{\"dealNumber\":\"DA001\"},\"owner\":{\"id\":1}").contentType(MediaType.APPLICATION_JSON)).andDo(print());
}
I cant past multiple parameters to the controller service , how can I avoid this ? 我无法将多个参数传递给控制器服务,如何避免这种情况?
You won't be able to pass multiple arguments annotated with @RequestBody annotation. 您将无法传递带有@RequestBody注释的多个参数。 The argument annotated with this annotation holds the whole request body and it can't be split into multiple. 带有此注释的参数保留了整个请求正文,并且不能拆分成多个。
What you can do is to have a wrapper to hold your Deal
and Owner
objects and you can pass that wrapper as a single request body argument. 您可以做的是拥有一个包装器来保存Deal
和Owner
对象,然后可以将该包装器作为单个请求正文参数传递。
For eg: 例如:
public class Wrapper {
private Deal deal;
private Owner owner;
//Getters and setters
}
And your controller's method: 和您的控制器的方法:
@RequestMapping(value = "/create", method = RequestMethod.POST)
public
@ResponseBody
GlobalResponse createDeal(@RequestBody Wrapper wrapper) {
// code here
}
Hope this makes sense. 希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.