springbootjson的简单介绍
简介:
Spring Boot 是一个非常流行的 Java 后端框架,实现了快速启动、高效运行、依赖管理等众多优点。其中,Spring Boot 的 JSON 处理能力也是备受开发者们的喜爱。本文将介绍 Spring Boot 中 JSON 的相关知识,包括 JSON 格式化、JSON 和 Java Bean 的转换、JSON 和 Map 的转换等内容。
多级标题:
一、JSON 格式化
二、Java Bean 和 JSON 的转换
三、Map 和 JSON 的转换
详细说明:
一、JSON 格式化
在 Spring Boot 中,通过 Jackson 来实现 JSON 格式化。Jackson 是一个普及度很高的 Java 序列化框架,支持多样化的 Java 对象序列化形式,非常适用于我们在 Spring Boot 中进行 JSON 的序列化、反序列化等操作。
1、使用 Jackson 格式化 JSON
在 Spring Boot 中使用 Jackson 格式化 JSON非常简单,只需要在 pom.xml 文件中添加 jackson-databind 依赖即可。接下来,我们将看一看在Spring Boot中如何使用 Jackson 来格式化 JSON 数据,示例如下:
```
@RestController
public class UserController {
@GetMapping("/user")
public User getUser(){
User user=new User();
user.setName("小明");
user.setAge(25);
return user;
}
```
针对以上代码,在浏览器中访问 /user,我们将返回以下 JSON 数据:
```
"name": "小明",
"age": 25
```
2、格式化 JSON 的配置
在实际开发中,我们有时可能需要格式化 JSON 输出。例如,将 JSON 数据进行缩进、去掉空格、加上换行等等。此时,我们可以在 application.properties 文件中添加以下配置代码:
```
# 格式化 JSON 输出
spring.jackson.serialization.indent-output=true
```
设置为 true 后,JSON 数据将以格式化的形式进行展示。如果需要将缩进量设置为两个空格,可以加上以下代码配置:
```
# 缩进两个空格
spring.jackson.serialization.indent-output=true
spring.jackson.serialization.indent-spaces=2
```
二、Java Bean 和 JSON 的转换
在实际开发中,我们有时需要将 Java Bean 和 JSON 数据进行相互转换。Spring Boot 中通过 Jackson 来实现 Java Bean 和 JSON 的互转。
1、Java Bean 转 JSON
如果要将 Java Bean 转为 JSON 数据,只需要在 Controller 中返回其对象即可,Spring Boot 会自动将其转变为 JSON 格式的数据。示例如下:
```
@RestController
public class UserController {
@GetMapping("/user")
public User getUser(){
User user=new User();
user.setName("小明");
user.setAge(25);
return user;
}
```
如果我们在浏览器中访问 /user,将得到如下的JSON 数据:
```
"name": "小明",
"age": 25
```
2、JSON 转 Java Bean
将 JSON 数据转换为 Java Bean 也非常简单,只需要使用 Jackson 将 JSON 数据转为对应类的对象即可。示例代码如下:
```
@RestController
public class UserController {
@GetMapping("/user")
public User getUser() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"name\": \"小明\", \"age\": 25 }";
User user = objectMapper.readValue(json, User.class);
return user;
}
```
针对以上代码,我们将得到如下的 JSON 数据:
```
"name": "小明",
"age": 25
```
三、Map 和 JSON 的转换
在开发中,我们有时也需要将 Map 和 JSON 数据进行相互转换,Spring Boot 也提供了相关的支持。
1、Map 转 JSON
将 Map 转为 JSON 数据非常简单,只需要使用 Jackson 将 Map 转为 JSON 串即可。以下示例代码展示了如何将 HashMap 转为 JSON 数据:
```
@RestController
public class TestController {
@GetMapping("/test")
public String getJson(){
Map
map.put("name","张三");
map.put("age",20);
map.put("address","广州市天河区");
ObjectMapper objectMapper = new ObjectMapper();
String json = null;
try {
json = objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return json;
}
```
请求 /test 接口,我们将得到以下 JSON 数据:
```
"name": "张三",
"age": 20,
"address": "广州市天河区"
```
2、JSON 转 Map
将 JSON 数据转为 Map 非常简单,只需要使用 Jackson 将 JSON 转为 Map 串即可。以下示例代码展示了如何将 JSON 数据转为 HashMap:
```
@RestController
public class TestController {
@GetMapping("/test")
public String getMap(){
String json="{\"name\":\"张三\",\"age\":20,\"address\":\"广州市天河区\"}";
ObjectMapper objectMapper = new ObjectMapper();
Map
try {
map = objectMapper.readValue(json, Map.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return map.toString();
}
```
请求 /test 接口,我们将得到以下 Map 数据:
```
name=张三,
age=20,
address=广州市天河区
```
总结:
本文介绍了 Spring Boot 中 JSON 的相关知识,包括 JSON 格式化、Java Bean 和 JSON 的转换、Map 和 JSON 的相互转换等内容。希望能够对读者有所帮助。