diff --git a/easy-enum-demo/pom.xml b/easy-enum-demo/pom.xml new file mode 100644 index 0000000..0d561be --- /dev/null +++ b/easy-enum-demo/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + io.github.luo-zhan + EasyEnum + 1.1.0-RELEASE + + + easy-enum-demo + + + 8 + 8 + UTF-8 + + + + io.github.luo-zhan + easy-enum + 1.1.0-RELEASE + + + io.github.luo-zhan + easy-enum-for-spring + 1.1.0-RELEASE + + + + + org.projectlombok + lombok + 1.18.26 + compile + + + + + org.springframework.boot + spring-boot-starter-web + 2.7.14 + + + org.springframework.boot + spring-boot-starter-test + 2.7.14 + + + + \ No newline at end of file diff --git a/easy-enum-demo/src/main/java/com/robot/dict/demo/Application.java b/easy-enum-demo/src/main/java/com/robot/dict/demo/Application.java new file mode 100644 index 0000000..d573063 --- /dev/null +++ b/easy-enum-demo/src/main/java/com/robot/dict/demo/Application.java @@ -0,0 +1,11 @@ +package com.robot.dict.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/easy-enum-demo/src/main/java/com/robot/dict/demo/bean/Teacher.java b/easy-enum-demo/src/main/java/com/robot/dict/demo/bean/Teacher.java new file mode 100644 index 0000000..b3e2e6f --- /dev/null +++ b/easy-enum-demo/src/main/java/com/robot/dict/demo/bean/Teacher.java @@ -0,0 +1,19 @@ +package com.robot.dict.demo.bean; + +import com.robot.dict.demo.enums.Sex; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Teacher { + + private Integer id; + + private String name; + private Sex sex; + + private String address; +} diff --git a/easy-enum-demo/src/main/java/com/robot/dict/demo/controller/Controller.java b/easy-enum-demo/src/main/java/com/robot/dict/demo/controller/Controller.java new file mode 100644 index 0000000..7a5c89f --- /dev/null +++ b/easy-enum-demo/src/main/java/com/robot/dict/demo/controller/Controller.java @@ -0,0 +1,34 @@ +package com.robot.dict.demo.controller; + + +import com.robot.dict.demo.bean.Teacher; +import com.robot.dict.demo.enums.Sex; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + @GetMapping("/json/serialize") + public Teacher getTeacher() { + // 观察接口响应中的sex属性值 + return new Teacher(1, "张三", Sex.MALE, "长沙"); + } + + @GetMapping("/json/deserialize") + public String addTeacher(@RequestBody Teacher teacher) { + return "json反序列化性别枚举,结果:" + teacher.getSex().getText(); + } + + @GetMapping("/deserialize") + public String listTeacher(Sex sex) { + return "枚举可以直接作为接口参数,结果:" + sex.getText(); + } + + @GetMapping("/serialize") + public Sex listTeacher() { + // 观察接口响应 + return Sex.MALE; + } +} diff --git a/easy-enum-demo/src/main/java/com/robot/dict/demo/enums/Sex.java b/easy-enum-demo/src/main/java/com/robot/dict/demo/enums/Sex.java new file mode 100644 index 0000000..2794387 --- /dev/null +++ b/easy-enum-demo/src/main/java/com/robot/dict/demo/enums/Sex.java @@ -0,0 +1,18 @@ +package com.robot.dict.demo.enums; + +import com.robot.dict.Dict; + +/** + * 性别枚举示例 + */ +public enum Sex implements Dict { + MALE(1, "男"), + FEMALE(2, "女"), + UNKNOWN(3, "未知"); + + Sex(Integer code, String text) { + // 一个init方法搞定 + init(code, text); + } + +}