Skip to content

Commit

Permalink
增加demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot committed Sep 25, 2023
1 parent f89dfa8 commit 096b7f2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
52 changes: 52 additions & 0 deletions easy-enum-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.luo-zhan</groupId>
<artifactId>EasyEnum</artifactId>
<version>1.1.0-RELEASE</version>
</parent>

<artifactId>easy-enum-demo</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum</artifactId>
<version>1.1.0-RELEASE</version>
</dependency>
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum-for-spring</artifactId>
<version>1.1.0-RELEASE</version>
</dependency>


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.14</version>
</dependency>
</dependencies>

</project>
11 changes: 11 additions & 0 deletions easy-enum-demo/src/main/java/com/robot/dict/demo/Application.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 19 additions & 0 deletions easy-enum-demo/src/main/java/com/robot/dict/demo/bean/Teacher.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions easy-enum-demo/src/main/java/com/robot/dict/demo/enums/Sex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.robot.dict.demo.enums;

import com.robot.dict.Dict;

/**
* 性别枚举示例
*/
public enum Sex implements Dict<Integer> {
MALE(1, "男"),
FEMALE(2, "女"),
UNKNOWN(3, "未知");

Sex(Integer code, String text) {
// 一个init方法搞定
init(code, text);
}

}

0 comments on commit 096b7f2

Please sign in to comment.