spring请求入参-Map类型

1.需求

入参接收为Map类型
单元测试写法?postman写法?

2.实现

示例:

2.1.入参接口写法

 @PostMapping("/a/v1/aaa")
    public void aMethod(@RequestParam(value = "aId") String aId,
                                            @RequestBody Map map){
        return;
    }

2.2.单元测试写法

		//构建post-Map体
		Map map = new HashMap<>();
        map.put("salesPrice",new BigDecimal("12"));
        //发送UT请求
        restQuoteMockMvc.perform(post("/a/v1/aaa")
            .param("aId", a.getId())
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(map)))
            .andDo(print())
            .andExpect(status().isOk());

2.3.postman测试入参格式

spring请求入参-Map类型_第1张图片

附录 TestUtils.class

public final class TestUtil {

    private static final ObjectMapper mapper = createObjectMapper();
   
    public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
        return mapper.writeValueAsBytes(object);
    }

}

你可能感兴趣的:(spring)