1 课程应用
1.1 统一响应处理
https://www.cnblogs.com/1446358788-qq/p/14296120.html
1.2 统一异常处理
https://www.cnblogs.com/1446358788-qq/p/14296122.html
2 实际应用
2.1 不同类型判断
List<StudentDto> list = (o1 instanceof List) ? (List)o1 : Collections.EMPTY_LIST; StudentDto dto = (o1 instanceof StudentDto) ? (StudentDto) o1 : new StudentDto(); String dto = (o1 instanceof String) ? (String) o1 : "";
- 测试类:
package com.ddwei.test; import com.ddwei.test.dto.StudentDto; import com.ddwei.test.service.StudentService; import com.ddwei.test.util.CommResult; import org.junit.Test; import java.util.Collections; import java.util.List; public class JavaTest<T> { StudentService service = new StudentService(); /** * 测试返回list情况 * @author weidoudou * @date 2021/8/27 20:39 * @param * @return void **/ @Test public void test1(){ CommResult<T> commResult = service.handl1(); Object o1 = commonHandlDismantl(commResult); List<StudentDto> list = (o1 instanceof List) ? (List)o1 : Collections.EMPTY_LIST; list.forEach((e)->System.out.println(e.getStuNo()+" "+e.getStuName())); } /** * 测试返回对象情况 * @author weidoudou * @date 2021/8/27 20:40 * @param * @return void **/ @Test public void test2(){ CommResult<T> commResult = service.handl2(); Object o1 = commonHandlDismantl(commResult); StudentDto dto = (o1 instanceof StudentDto) ? (StudentDto) o1 : new StudentDto(); System.out.println(dto.getStuNo()+" "+dto.getStuName()); } /** * 测试返回字符串情况 * @author weidoudou * @date 2021/8/27 20:40 * @param * @return void **/ @Test public void test3(){ CommResult<T> commResult = service.handl3(); Object o1 = commonHandlDismantl(commResult); String dto = (o1 instanceof String) ? (String) o1 : ""; System.out.println(dto); } public T commonHandlDismantl(CommResult<T> commResult){ return commResult.getData(); } }
- service
package com.ddwei.test.service; import com.ddwei.test.dto.StudentDto; import com.ddwei.test.util.CommResult; import java.util.ArrayList; import java.util.List; public class StudentService<T> { /** * 用来测试封装列表 * @author weidoudou * @date 2021/8/27 20:46 * @param * @return com.ddwei.test.util.CommResult<T> **/ public CommResult<T> handl1(){ //定义列表相关 StudentDto studentDto1 = new StudentDto("1001","张三"); StudentDto studentDto2 = new StudentDto("1002","李四"); List<StudentDto> list = new ArrayList<StudentDto>(); list.add(studentDto1); list.add(studentDto2); //定义公共返回类 CommResult commResult = new CommResult(list); return commResult; } /** * 用来测试封装实体类 * @author weidoudou * @date 2021/8/27 20:48 * @param * @return com.ddwei.test.util.CommResult<T> **/ public CommResult<T> handl2(){ StudentDto studentDto = new StudentDto("9001","白骨精"); CommResult commResult = new CommResult(studentDto); return commResult; } /** * 用来测试封装字符串 * @author weidoudou * @date 2021/8/27 20:51 * @param * @return com.ddwei.test.util.CommResult<T> **/ public CommResult<T> handl3(){ String str = "we Are Friends"; CommResult commResult = new CommResult(str); return commResult; } }
- 封装类
package com.ddwei.test.util; import lombok.Data; @Data public class CommResult<T> { private String code; private String message; private T data; public CommResult(T data){ this.code = "0"; this.message = ""; this.data = data; } }
- 实体类
package com.ddwei.test.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @NoArgsConstructor @AllArgsConstructor @Data public class StudentDto implements Serializable { private String stuNo; private String stuName; }
诸葛