【Spring Boot丨(11 )】json的集成
发布人:shili8
发布时间:2025-02-26 09:08
阅读次数:0
**Spring Boot与JSON的集成**
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于Web服务、移动应用程序等场景中。Spring Boot提供了对JSON的支持,使得开发者能够方便地将Java对象转换为JSON数据,并且可以使用JSON来传递数据。
**1. JSON的基本概念**
JSON是一种文本格式,用于表示JavaScript中的对象和数组。它由一系列的键值对组成,每个键值对之间用逗号分隔。例如:
json{ "name": "John", "age":30, "city": "New York" }
**2. Spring Boot与JSON的集成**
Spring Boot提供了一个名为`ObjectMapper`的类,用于将Java对象转换为JSON数据。我们可以使用`@JsonFormat`注解来指定JSON的格式。
### 示例代码
javaimport com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; public class User { private String name; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") private int age; private String city; public User(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } public class Main { public static void main(String[] args) throws Exception { User user = new User("John",30, "New York"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(user); System.out.println(json); } }
在上面的示例中,我们定义了一个`User`类,包含三个属性:`name`、`age`和`city`。我们使用`@JsonFormat`注解来指定JSON的格式。
### JSON的读取Spring Boot提供了一个名为`ObjectMapper`的类,用于将JSON数据转换为Java对象。我们可以使用`readValue()`方法来读取JSON数据。
javaimport com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) throws Exception { String json = "{"name":"John","age":30,"city":"New York"}"; ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(json, User.class); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getCity()); } }
在上面的示例中,我们使用`readValue()`方法来读取JSON数据,并将其转换为一个`User`对象。
**3. JSON的序列化和反序列化**
Spring Boot提供了对JSON的序列化和反序列化支持。我们可以使用`ObjectMapper`类来实现这些功能。
### 示例代码
javaimport com.fasterxml.jackson.databind.ObjectMapper; public class User { private String name; private int age; private String city; public User(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } public class Main { public static void main(String[] args) throws Exception { User user = new User("John",30, "New York"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(user); System.out.println(json); User user2 = mapper.readValue(json, User.class); System.out.println(user2.getName()); System.out.println(user2.getAge()); System.out.println(user2.getCity()); } }
在上面的示例中,我们使用`ObjectMapper`类来实现JSON的序列化和反序列化。
**4. JSON的验证**
Spring Boot提供了对JSON的验证支持。我们可以使用`@Valid`注解来指定JSON的验证规则。
### 示例代码
javaimport javax.validation.constraints.NotBlank; public class User { @NotBlank(message = "name cannot be blank") private String name; private int age; private String city; public User(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } public class Main { public static void main(String[] args) throws Exception { User user = new User("John",30, "New York"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(user); System.out.println(json); try { User user2 = mapper.readValue(json, User.class); System.out.println(user2.getName()); System.out.println(user2.getAge()); System.out.println(user2.getCity()); } catch (Exception e) { System.out.println(e.getMessage()); } } }
在上面的示例中,我们使用`@Valid`注解来指定JSON的验证规则。
**5. JSON的过滤**
Spring Boot提供了对JSON的过滤支持。我们可以使用`ObjectMapper`类来实现JSON的过滤。
### 示例代码
javaimport com.fasterxml.jackson.databind.ObjectMapper; public class User { private String name; private int age; private String city; public User(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } public class Main { public static void main(String[] args) throws Exception { User user = new User("John",30, "New York"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(user); System.out.println(json); JsonFilter filter = new JsonFilter() { @Override public boolean isIncludeProperty(String propertyName) { return !propertyName.equals("age"); } }; String filteredJson = mapper.writeValueAsString(user, filter); System.out.println(filteredJson); } }
在上面的示例中,我们使用`ObjectMapper`类来实现JSON的过滤。
**6. JSON的排序**
Spring Boot提供了对JSON的排序支持。我们可以使用`@JsonPropertyOrder`注解来指定JSON的排序规则。
### 示例代码
javaimport com.fasterxml.jackson.annotation.JsonPropertyOrder; public class User { private String name; @JsonPropertyOrder("age") private int age; private String city; public User(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } public class Main { public static void main(String[] args) throws Exception { User user = new User("John",30, "New York"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(user); System.out.println(json); try { User user2 = mapper.readValue(json, User.class); System.out.println(user2.getName()); System.out.println(user2.getAge()); System.out.println(user2.getCity()); } catch (Exception e) { System.out.println(e.getMessage()); } } }
在上面的示例中,我们使用`@JsonPropertyOrder`注解来指定JSON的排序规则。
**7. JSON的转换**
Spring Boot提供了对JSON的转换支持。我们