MockMVC在使用POST请求时,会在请求参数中的字符串添加额外的引号。

huangapple go评论84阅读模式
英文:

MockMVC adds additional quotes with string in request param with post request

问题

MockMvc在请求构建器中的param()中传入字符串时会在字符串两端添加引号,就像以下示例一样:

// 初始化mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

ObjectNode tweets = ((ObjectNode) result.getRequest().getAttribute("tweets"));

String query = tweets.get("query").toString();
String nextToken = tweets.get("meta").get("next_token").toString();

mockMvc.perform(MockMvcRequestBuilders.post("/next")
                    .param("query", query)
                    .param("next_token", nextToken)
                    .accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$.isError", is("N")))
                    .andReturn();

如果控制器中接收到的query"#GoGreen"next_token"wefw234234ewf234",则在控制器中的query"\"#GoGreen\""next_token"\"wefw234234ewf234\""

@PostMapping("/next")
@ResponseBody
public ResponseEntity<Object> nextPageTrendingTweets(@RequestParam("query") String query,
                                                     @RequestParam("next_token") String nextToken)

也许在初始化mockMvc时我漏掉了一些东西。我搜索了这个问题,但找不到任何解决方案。

英文:

MockMvc adds quotes at the ends of string when passed in param() in request builder like following

// initialization of mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

ObjectNode tweets = ((ObjectNode) result.getRequest().getAttribute(&quot;tweets&quot;));

String query = tweets.get(&quot;query&quot;).toString();
String nextToken = tweets.get(&quot;meta&quot;).get(&quot;next_token&quot;).toString();

mockMvc.perform(MockMvcRequestBuilders.post(&quot;/next&quot;)
    				.param(&quot;query&quot;, query)
    				.param(&quot;next_token&quot;, nextToken)
    				.accept(MediaType.APPLICATION_JSON))
    				.andExpect(status().isOk())
    				.andExpect(jsonPath(&quot;$.isError&quot;, is(&quot;N&quot;)))
    				.andReturn();

if query is &quot;#GoGreen&quot; and next_token is &quot;wefw234234ewf234&quot; are received in controller is
query = &quot;\&quot;#GoGreen\&quot;&quot; and next_token = &quot;\&quot;wefw234234ewf234\&quot;&quot;

    @PostMapping(&quot;/next&quot;) @ResponseBody
	public ResponseEntity&lt;Object&gt; nextPageTrendingTweets(@RequestParam(&quot;query&quot;) String query,
                                                         @RequestParam(&quot;next_token&quot;) String nextToken)

Maybe I'm missing something when initializing mockMvc. I searched about this problem but couldn't find any solution.

答案1

得分: 1

问题不在于mockMvc,而在于您从“ObjectNode”中提取数据的方式有问题。您使用了.toString()方法来获取字符串数据。然而,这是不正确的,因为ObjectNode.get()方法返回对象,而.toString()将其转换为字符串表示,这就是额外的引号所在。因此,解决问题的方法是使用ObjectNode.get().asText()方法,它将返回对象中的确切字符串,而不进行任何修改或转换。

英文:

The problem is not with mockMvc but it is with how you're extracting data from ObjectNode "tweets". You've used .toString() method to get string data. However, it is incorrect because ObjectNode.get() method returns object and .toString() converts it into string representation that's why extra quotes. So the solution of the problem is use ObjectNode.get().asText() method which will return that exact string in the object without any modification or conversion.

答案2

得分: 0

片段部分(#foo)完全位于浏览器内部,不会包含在HTTP请求中。根本不要发送它。

英文:

The fragment part (#foo) lives entirely inside the browser and is not passed in HTTP requests. Don't send it at all.

huangapple
  • 本文由 发表于 2020年10月15日 03:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64359909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定