脚本宝典收集整理的这篇文章主要介绍了java springboot 自定义异常,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
- 自定义返回result
public class CustomResult { /** * 状态响应码 */ PRivate String code; /** * 响应信息 */ private String message; /** * 响应数据 */ @JsonInclude(JsonInclude.Include.NON_NULL) private Object data; /** * 数据总数 */ @JsonInclude(JsonInclude.Include.NON_NULL) private Long totalCount; public CustomResult setCode(CustomResultCode customResultCode) { this.code = customResultCode.code(); return this; } public String getCode() { return code; } public String getMessage() { return message; } public CustomResult setMessage(String message) { this.message = message; return this; } public Object getData() { return data; } public CustomResult setData(Object data) { this.data = data; return this; } public Long getTotalCount() { return totalCount;} public CustomResult setTotalCount(Long totalCount) { this.totalCount = totalCount; return this; } }
- 自定义错误code
public enum CustomResultCode { Custom_SUCCESS("1"), //成功 Custom_Fail("0"); // 失败 private final String code; CustomResultCode(String code) { this.code = code; } public String code() { return code; } }
- 测试
@RequestMapping("test-custom-exception") public void testCustomException() { throw new CustomException("自定义错误"); }
- 结果
{"code":"0","message":"自定义错误"}
以上是脚本宝典为你收集整理的java springboot 自定义异常全部内容,希望文章能够帮你解决java springboot 自定义异常所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。