API返回对象ResponseDTO<T>,包括以下部分
ResponseDTO源码如下:
package com.study.web.dto; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class ResponseDTO<T> { /** * 调用是否成功 */ @Builder.Default private boolean result = true; /** * 状态码 */ @Builder.Default private String code = "200"; /** * 结果消息 */ @Builder.Default private String msg = ""; /** * 响应数据 */ private T data; /** * <p> 返回成功结果,无返回结果集 </p > * * @return ResponseDTO<T> */ public static <T> ResponseDTO<T> success() { return success(null); } /** * <p> 返回成功结果,有返回结果集 </p > * * @return ResponseDTO<T> */ @SuppressWarnings("unchecked") public static <T> ResponseDTO<T> success(T data) { return ((ResponseDTO<T>) ResponseDTO.builder().result(true).code("200").msg("").data(data).build()); } /** * <p> 返回失败结果,无返回结果集 </p > * * @return ResponseDTO<T> */ public static <T> ResponseDTO<T> error() { return error(null); } /** * <p> 返回失败结果,有返回结果集 </p > * * @return ResponseDTO<T> */ @SuppressWarnings("unchecked") public static <T> ResponseDTO<T> error(T data) { return ((ResponseDTO<T>) ResponseDTO.builder().result(false).data(data).build()); } }
示例代码:
1、无返回Data
/** * 无返回Data */ @PostMApping("/testVoid") public ResponseDTO<Void> testVoid(){ ResponseDTO<Void> result = ResponseDTO.success(); try { //业务代码 studyService.testVoid(); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; }
2、返回对象
/** * 返回对象 */ @PostMapping("/testString") public ResponseDTO<String> testString(){ ResponseDTO<String> result = ResponseDTO.success(); try { //业务代码 String data = studyService.testString(); result.setData(data); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; }
3、返回对象列表
/** * 返回对象列表 */ @PostMapping("/testList") public ResponseDTO<List<String>> testList(){ ResponseDTO<List<String>> result = ResponseDTO.success(); try { //业务代码 List<String> data = studyService.testList(); result.setData(data); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; }
在这里我以String型来代表任何类型的对象。
建议:controller层,接口级代码,不要涉及过多业务代码,业务代码都放在service层处理并返回。
controller层源码:
package com.study.web.controller; import JAVA.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.study.service.StudyService; import com.study.web.dto.ResponseDTO; import lombok.extern.slf4j.Slf4j; @RestController @Slf4j @RequestMapping("/study") public class StudyController { @Autowired StudyService studyService; /** * 无返回Data */ @PostMapping("/testVoid") public ResponseDTO<Void> testVoid(){ ResponseDTO<Void> result = ResponseDTO.success(); try { //业务代码 studyService.testVoid(); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; } /** * 返回对象 */ @PostMapping("/testString") public ResponseDTO<String> testString(){ ResponseDTO<String> result = ResponseDTO.success(); try { //业务代码 String data = studyService.testString(); result.setData(data); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; } /** * 返回对象列表 */ @PostMapping("/testList") public ResponseDTO<List<String>> testList(){ ResponseDTO<List<String>> result = ResponseDTO.success(); try { //业务代码 List<String> data = studyService.testList(); result.setData(data); } catch (Exception e) { log.error("处理异常", e); result.setResult(false); result.setCode("500"); result.setMsg("异常"); } return result; } }
service层源码:
package com.study.service; import java.util.List; import org.springframework.stereotype.Service; @Service public class StudyService { public void testVoid(){ } public String testString(){ return null; } public List<String> testList(){ return null; } }