简体   繁体   English

为什么我们在mockmvc中测试POST方法时需要将请求体设置为UTF-8字符串

[英]Why do we need to set the request body as a UTF-8 string when testing the POST method in mockmvc

I would like to know how to test POST method so I am reading this article and this test looks like this:我想知道如何测试 POST 方法,所以我正在阅读这篇文章,这个测试看起来像这样:

@Test
public void createEmployeeAPI() throws Exception 
{
  mvc.perform( MockMvcRequestBuilders
      .post("/employees")
      .content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))
      .contentType(MediaType.APPLICATION_JSON)
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isCreated())
      .andExpect(MockMvcResultMatchers.jsonPath("$.employeeId").exists());
}
 
public static String asJsonString(final Object obj) {
    try {
        return new ObjectMapper().writeValueAsString(obj);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

I don't understand what this line of code is for and what exactly it does: .content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))我不明白这行代码是做什么用的以及它到底做了什么: .content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))

content(String content) from the documentation:文档中的content(String content)

"Set the request body as a UTF-8 String. If content is provided and contentType(MediaType) is set to application/x-www-form-urlencoded, the content will be parsed and used to populate the request parameters map." “将请求正文设置为 UTF-8 字符串。如果提供了内容并且 contentType(MediaType) 设置为 application/x-www-form-urlencoded,则内容将被解析并用于填充请求参数 map。”

I don't understand what this line of code is for and what exactly it does: .content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))我不明白这行代码是做什么用的以及它到底做了什么: .content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))

Assume that your rest api expects a POST request with Json Body.假设您的 rest api 期望一个带有 Json Body 的 POST 请求。

The method content expects a body as string and asJsonString return a json string from the object EmployeeVO with helper ObjectMapper from Jackson library.方法content需要一个字符串作为主体, asJsonString从 object EmployeeVO 中返回一个 json 字符串,并带有来自 Jackson 库的帮助器 ObjectMapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保证Java POST请求字符串/文本为UTF-8编码 - How to guarantee a java POST request string / text to be UTF-8 encoding 使用 MockMvc.jsonpath() 方法测试 API 响应正文时如何在括号 [] 内输入 - How to enter inside a bracket [] when testing API response body using MockMvc .jsonpath() method 为什么我必须将utf-8参数String编码为iso-Latin,然后解码为utf-8才能获得Java utf-8 String? - Why do I have to encode a utf-8 parameter String to iso-Latin and then decode as utf-8 to get Java utf-8 String? Jersey REST WS - 请求正文 UTF-8 - Jersey REST WS - request body UTF-8 除了测试,为什么我们需要Dagger 2? - Beside testing, why do we need Dagger 2? 当 set 在 Java 中已经是原子的时,为什么我们需要 compareAndSet? - Why do we need to compareAndSet when set is already atomic in Java? MockMvc测试OPTIONS请求 - MockMvc testing OPTIONS request Spring Boot:MockMvc 为 POST 请求返回空体 - Spring Boot: MockMvc returning empty body for POST request UTF-8 格式的 JAVA Http POST 请求 - JAVA Http POST request in UTF-8 为什么当GET也可以充当POST时,为什么需要在RestFul中使用GET和POST方法 - Why we need GET and POST method in RestFul when GET can behave as POST also
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM