# Jackson的使用
###### tags: `SpringMVC-json`
1.使用Jackson,需要先在pom.xml導入jar包
```xml=
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
```
2.配置SpringMVC的web.xml環境
```xml=
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springmvc-05-json</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>
```
3.springmvc-servlet.xml
```xml=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.kuang.controller"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
```
4.編寫一個User實體類
```java=
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//需要导入lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
}
```
5.編寫一個控制器
```java=
@Controller
public class UserController {
@RequestMapping("/j1")
@ResponseBody //他就不會走視圖解析器,會直接返回一個字符串
public String json1(){
//創建一個對象
User user = new User("戀戀",10,"女");
return user.toString();
}
}
```
5.結果,返回toString的字符串

### 使用jackson
```java=
@Controller
public class UserController {
@RequestMapping("/j1")
@ResponseBody //他就不會走視圖解析器,會直接返回一個字符串
public String json1() throws JsonProcessingException {
//jackson,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//創建一個對象
User user = new User("戀戀",10,"女");
String str = mapper.writeValueAsString(user);
return str;
}
}
```
#### 變成json風格的字符串(有加"")

### @RestController
不走視圖解析器,直接回傳字符串
```java=
@RestController
public class UserController {
@RequestMapping("/j1")
public String json1() throws JsonProcessingException {
//jackson,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//創建一個對象
User user = new User("戀戀",10,"女");
String str = mapper.writeValueAsString(user);
return str;
}
}
```
結果

### 呈現集合數據
```java=
@RequestMapping("/j2")
// @ResponseBody
public String json2() throws JsonProcessingException {
//jackson,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
List<User> userList = new ArrayList<User>();
User user1 = new User("戀戀1",10,"女");
User user2 = new User("戀戀2",10,"女");
User user3 = new User("戀戀3",10,"女");
User user4 = new User("戀戀4",10,"女");
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
String str = mapper.writeValueAsString(userList);
return str;// new ObjectMapper().writeValueAsString(userList);
}
```
#### 結果

### 表示時間
```java=
@RequestMapping("/j3")
public String json3() throws JsonProcessingException {
//jackson,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
Date date = new Date();
//ObjectMapper,時間解析後的默認格式為:TimeStamp
return mapper.writeValueAsString(date);
}
```
#### 結果

### 設置時間的格式1
```java=
@RequestMapping("/j3")
public String json3() throws JsonProcessingException {
//jackson,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
Date date = new Date();
//自定義日期的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//ObjectMapper,時間解析後的默認格式為:TimeStamp(時間戳)
return mapper.writeValueAsString(sdf.format(date));
}
```

### 使用ObjectMapper來格式化輸出
```java=
@RequestMapping("/j3")
public String json3() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//不使用時間戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//自定義日期的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
Date date = new Date();
//ObjectMapper,時間解析後的默認格式為:TimeStamp(時間戳)
return mapper.writeValueAsString(sdf.format(date));
}
```
