API返回對象ResponseDTO<T>,包括以下部分
- result:調用是否成功
- code:狀態碼
- msg:結果消息
- data:響應數據,泛型<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; } }