scala case class Person(name: String, age: Int) <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-scala_${scala.version}</artifactId> <version>${jackson.module.scala.version}</version> </dependency> import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.scala.DefaultScalaModule; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new DefaultScalaModule()); import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.scala.DefaultScalaModule; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new DefaultScalaModule()); Person person = new Person("John Doe", 30); try { String jsonString = objectMapper.writeValueAsString(person); System.out.println(jsonString); Person deserializedPerson = objectMapper.readValue(jsonString, Person.class); System.out.println(deserializedPerson); } catch (Exception e) { e.printStackTrace(); }


上一篇:
下一篇:
切换中文