众所周知,现在的前后端交互基本都是采用标准的JSON结构{"code":200,"message":"success","data":null}不知道@ControllerAdvice接口返回之前的注释像这样@GetMapping("/")publicBaseResponsetest(){StringtestMsg=testService.getTestMsg();返回BaseResponse.ok(testMsg);}@GetMapping("/findUser/{id}")publicBaseResponsefindUserById(@PathVariable("id")LonguserId){Useruser=testService.findUserById(userId);返回BaseResponse.ok(用户);}BaseResponse是统一返回的对象@Data@EqualsAndHashCode@NoArgsConstructorpublicclassBaseResponse{/***Responsecode*/privateIntegercode;/***响应信息*/privateStringmsg;/***响应数据*/私有T数据;publicBaseResponse(Integerstatus,Stringmsg,Tdata){this.code=status;this.msg=味精;这个。数据=数据;}@NonNullpublicstaticBaseResponseok(@NullableStringmsg,@NullableTdata){returnnewBaseResponse<>(HttpStatus.OK.value(),msg,data);}}@NonNullpublicstaticBaseResponseok(@NullableStringmsg){returnok(msg,null);}publicstaticBaseResponseok(@NullableTdata){returnnewBaseResponse<>(HttpStatus.OK.value(),HttpStatus.OK.getReasonPhrase(),data);}}知道@ControllerAdvice注解后,接口返回是这样的@GetMapping("/")publicStringtest(){StringtestMsg=testService.getTestMsg();返回测试消息;}@GetMapping("/findUser/{id}")publicUserfindUserById(@PathVariable("id")LonguserId){Useruser=testService.findUserById(userId);返回用户;}使用@ControllerAdvice注解拦截Controller层的所有接口,然后使用BaseResponse进行统一封装,这样就不需要在每个Controller接口都使用BaseResponse来封装@ControllerAdvice("com.test.controller")公共类CommonResultControllerAdvice实现ResponseBodyAdvice