使用MappingJackson2HttpMessageConverter把java对象转换成json字符串
如下图:
运行结果如下图:
代码如下:
/**
* @author 望轩
* @createDate 2024/6/27 15:27
* 把java对象转换成json字符串
*/
public class EntityToJson {
public static void main(String[] args) throws JsonProcessingException {
User user = new User();
user.setAddress("sss");
user.setAge("18");
user.setName("zs");
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = messageConverter.getObjectMapper();
String convertResult = objectMapper.writeValueAsString(user);
System.out.println(convertResult);
}
}