json anno
This commit is contained in:
parent
61fd6cae54
commit
b21b8b1ea8
6
pom.xml
6
pom.xml
@ -46,6 +46,12 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.35</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
34
src/main/java/stu/json/JsonAnno.java
Normal file
34
src/main/java/stu/json/JsonAnno.java
Normal file
@ -0,0 +1,34 @@
|
||||
package stu.json;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import stu.json.other.BudgetDeserializer;
|
||||
import stu.json.other.BudgetSerializer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author cuiJiaWang
|
||||
* @Create 2025-02-08 12:40
|
||||
*/
|
||||
@JsonIgnoreProperties({"name", "age"})
|
||||
public class JsonAnno {
|
||||
private String name;
|
||||
private String age;
|
||||
@JsonIgnore
|
||||
private String mail;
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date time;
|
||||
|
||||
@JsonSerialize(using = BudgetSerializer.class)
|
||||
@JsonDeserialize(using = BudgetDeserializer.class)
|
||||
private BigDecimal applyBudget;
|
||||
|
||||
@JsonProperty("user_name")
|
||||
private String userName; // {"user_name":"aaa"}
|
||||
}
|
||||
35
src/main/java/stu/json/JsonAnno.md
Normal file
35
src/main/java/stu/json/JsonAnno.md
Normal file
@ -0,0 +1,35 @@
|
||||
## Json常用注解
|
||||
### 忽略
|
||||
```java
|
||||
@JsonIgnore
|
||||
@JsonIgnoreProperties
|
||||
@JsonIgnoreType // 标注在类上,当其他类有该类作为属性时,该属性将被忽略。
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL) // 只在序列化生效
|
||||
```
|
||||
- `@JsonIgnore`
|
||||
注解用来忽略某些字段,可以用在变量或者Getter方法上,用在Setter方法时,和变量效果一样。这个注解一般用在我们要忽略的字段上
|
||||
- `@JsonIgnoreProperties`
|
||||
可以应用在类级别的,也可以用在变量和方法
|
||||
`ignoreUnknown = true` 忽略类中不存在的字段
|
||||
`{ “password”, “secretKey” }` 指定要忽略的字段
|
||||
|
||||
### 格式化
|
||||
```java
|
||||
@JsonFormat
|
||||
```
|
||||
|
||||
### 序列化及反序列化
|
||||
该注解只在json序列化和反序列化的时候触发,其他时候并不生效
|
||||
```java
|
||||
@JsonSerialize // 序列化,用于getter方法
|
||||
@JsonDeserialize // 反序列化,用于字段或setter方法上
|
||||
```
|
||||
### 映射
|
||||
```java
|
||||
@Transient
|
||||
@JsonProperty // 指定某个属性和json映射的名称
|
||||
@JsonSetter // 只在反序列化生效,类似 @JsonProperty
|
||||
```
|
||||
如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架默认其注解为@Basic
|
||||
|
||||
@JsonSetter 也可以解决 json 键名称和 java pojo 字段名称不匹配的问题
|
||||
37
src/main/java/stu/json/other/BudgetDeserializer.java
Normal file
37
src/main/java/stu/json/other/BudgetDeserializer.java
Normal file
@ -0,0 +1,37 @@
|
||||
package stu.json.other;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Author cuiJiaWang
|
||||
* @Create 2025-02-08 13:11
|
||||
*/
|
||||
@Slf4j
|
||||
public class BudgetDeserializer extends JsonDeserializer<BigDecimal> {
|
||||
@Override
|
||||
public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
|
||||
try {
|
||||
if (jsonParser == null || jsonParser.getText() == null) {
|
||||
return null;
|
||||
}
|
||||
String s = jsonParser.getText();
|
||||
BigDecimal format = new BigDecimal(StringUtils.hasLength(s) ? "0" : s);
|
||||
|
||||
// 万元转元
|
||||
format = format.multiply(new BigDecimal("10000"));
|
||||
log.debug("万元格式化元:前 {}, 后 {}", s, format);
|
||||
return format;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/main/java/stu/json/other/BudgetSerializer.java
Normal file
29
src/main/java/stu/json/other/BudgetSerializer.java
Normal file
@ -0,0 +1,29 @@
|
||||
package stu.json.other;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* @Author cuiJiaWang
|
||||
* @Create 2025-02-08 13:07
|
||||
*/
|
||||
@Slf4j
|
||||
public class BudgetSerializer extends JsonSerializer<BigDecimal> {
|
||||
|
||||
@Override
|
||||
public void serialize(BigDecimal s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
BigDecimal format = s;
|
||||
if (format != null) {
|
||||
// 元转万元
|
||||
format = format.divide(new BigDecimal("10000"), 4, RoundingMode.HALF_UP);
|
||||
log.debug("元格式化万元:前 {}, 后 {}", s, format);
|
||||
}
|
||||
jsonGenerator.writeNumber(format);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user