Java 正确设置 MockHttpServletRequestBuilder 的 RequestBody

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22076276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:26:17  来源:igfitidea点击:

Setting RequestBody properly of MockHttpServletRequestBuilder

javaspring-mvc

提问by jlars62

I am simply trying to test a Spring controller method using a MockHttpServletRequestBuilder. The signature of the controller method looks like this:

我只是尝试使用MockHttpServletRequestBuilder. 控制器方法的签名如下所示:

 @RequestMapping(value = "/assignTeamsToUsers", method = RequestMethod.POST)
 public @ResponseBody String assignUsersToTeams(Model model, @RequestBody MultiValueMap<String, String> ids).

In my test case I have:

在我的测试用例中,我有:

MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
MockHttpServletRequestBuilder request = createRequest(uri, method);

//set up request object...Not sure how??
//My current attempt:
     String body = "userIds[]=0&teamIds[]=0";
     request.content(body);
     request.accept(MediaType.ALL);
     request.contentType(MediaType.APPLICATION_FORM_URLENCODED);

ResultActions resultActions = mockMvc.perform(request);

EDIT: Showing createRequest. method= "POST"

编辑:显示 createRequest。method= "发布"

private MockHttpServletRequestBuilder createRequest (String uri, String method) {
    MockHttpServletRequestBuilder builder = null;

    if("GET".equalsIgnoreCase(method))
        builder = MockMvcRequestBuilders.get(uri);
    else if("POST".equalsIgnoreCase(method))
        builder = MockMvcRequestBuilders.post(uri);
    else
        Assert.fail("Unsupported method!");

    //We always create requests for user Manager
    builder.header("securityRole", Role.Manager.getDisplayName());

    return builder;

}

I know the uri and method are correct. My problem is I am getting a 415error code from Spring. Basically, I do not know how to set up the requestobject to have the appropriate @RequestBodyfor the MultiValueMap. I have tried alot of variations of setting request.content, setting the request.accept, request.contentType, request.characterEncoding, and still every time I get a 415 error.

我知道 uri 和方法是正确的。我的问题是我从 Spring收到415错误代码。基本上,我不知道如何设置的request对象有适当@RequestBodyMultiValueMap。我已经尝试了很多设置 request.content、设置 request.accept、request.contentType、request.characterEncoding 的变体,但仍然每次出现 415 错误。

If it is any help, I can successfully Post to this endpoint using the web interface, and here is what the request looks like in chrome:

如果有任何帮助,我可以使用 Web 界面成功发布到此端点,这是请求在 chrome 中的样子:

enter image description here

在此处输入图片说明

采纳答案by jlars62

I ended up solving this by doing the following:

我最终通过执行以下操作解决了这个问题:

MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("whatever url");
request.contentType(MediaType.APPLICATION_FORM_URLENCODED);

//set key value pairs
//also the keys do not have to be unique, two keys of the same value will both get added 
request.param("key", "value");

MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
ResultActions resultActions = mockMvc.perform(request);
// make sure response is valid

Hope this can lead someone else in the right direction. Thanks

希望这可以引导其他人朝着正确的方向前进。谢谢