我习惯了JAX-RS,并想使用Spring MVC里面我的测试客户端发送请求时,并与响应工作,即有类似的舒适性。Spring MVC的测试,MockMVC:便利对象转换为/从JSON
在服务器(控制器)侧我与自动转换很高兴,即它足以仅仅返回的对象实例,并且具有在JSON发送到客户端所产生的HTTP响应。
您能否告诉我如何解决在这些片段中将objectInstance
转换为jsonString
或反之的手动过程?如果可能的话,我还想跳过手动配置内容类型。
String jsonStringRequest = objectMapper.writeValueAsString(objectInstance);
ResultActions resultActions = mockMvc.perform(post(PATH)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonStringRequest)
)
String jsonStringResponse = resultActions.andReturn().getResponse().getContentAsString();
Some objectInstanceResponse = objectMapper.readValue(jsonStringResponse, Some.class);
为了进行比较,与JAX-RS客户端API,我可以使用request.post(Entity.entity(objectInstance, MediaType.APPLICATION_JSON_TYPE)
轻松发送对象和读取使用response.readEntity(Some.class);