简介
Web 开发中无论是 MVC 还是 DDD 架构 Controller 层都是系统的门面,既对外的接口,对内的接口,一般情况下任何错误必须组织在 Controller 层
如何作
在 Controller 层中的接口使用 try-catch
@Slf4j
@RestController("/")
@RequiredArgsConstructor
public class TestController {
private final TestRepository testRepository;
@GetMapping
public Response<String> index() {
try {
testRepository.save();
return new Response<>();
} catch (Exception e) {
return new Response<>();
}
}
}
另外为避免前端和后端相互干扰我们还应积极使用 dto 、entity 、PO对象
public Response<List<MonitorDataMapDTO>> queryMonitorDataMapEntityList() {
try {
List<MonitorDataMapEntity> monitorDataMapEntities = logAnalyticalService.queryMonitorDataMapEntityList();
doEntityToDto()
}
return Response.<List<MonitorDataMapDTO>>builder()
.code("0000")
.info("调用成功")
.data(monitorDataMapDTOS)
.build();
} catch (Exception e) {
return Response.<List<MonitorDataMapDTO>>builder()
.code("0001")
.info("调用失败")
.build();
}
}
@RequestMapping(value = "draw", method = RequestMethod.POST)
@Override
public Response<ActivityDrawResponseDTO> draw(@RequestBody ActivityDrawRequestDTO request) {
try {
if (doCheckParameter()) {
throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
}
UserRaffleOrderEntity orderEntity = raffleActivityPartakeService.thisMethodWillQueryEntityByRequestId()
return something();
} catch (AppException e) {
return something();
} catch (Exception e) {
return something();
}
}