How to Converte Java objects to jsonnode in Jackson (Four Methods)

Can I convert Java objects to jsonnode objects directly
the only way to solve this problem is to convert the Java object to string and then to jsonnode

ObjectMapper mapper = new ObjectMapper();
//Method 1
String json = mapper.writeValueAsString(Java Object);
JsonNode jsonNode = mapper.readTree(json);
//Method 2
JsonNode jsonNode = mapper.valueToTree(Java Object);
//Method 3
JsonNode jsonNode = mapper.convertValue(Java Object, JsonNode.class);

Examples

	/**
	 * Converting Java objects to JsonNode in Jackson
	 *
	 * @param object
	 * @return
	 */
	public static JsonNode getJsonNode(Object object) {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.convertValue(object, JsonNode.class);

	}

Read More: