我試圖做一個@PutMapping,我將把所有的邏輯放在服務里面來改變屬性,但是我遇到了一個問題。我怎樣才能從我的requestDTO中獲取值在類中播放,并在服務中而不是在控制器中進行這些更改?
請求資料與回應和類相同
private Long id;
private String program;
我的控制器和服務實作
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
final Long id,
final ProgramRequestDto programRequestDto) throws UpdateDataException {
ProgramResponseDto programResponseDto = ProgramMapper.INSTANCE.programToProgramResponseDTO(
programService.Update(id))。
log.info(LocaleContext.format("response.success",
(new Object() {
}.getClass().getEnclosingMethod().getName())。
HttpStatus.OK.toString())。
return status(HttpStatus.OK).body(programResponseDto)。
}
public Program Update(Long id) throws UpdateDataException {
Program program = null。
try {
Optional<Program> programDB = programRepository.findById(id)。
if (programDB.isPresent() ) {
program = programDB.get();
ProgramRequestDto programRequestDto = ProgramRequestDto.builder().build()。
program.setProgram(programRequestDto.getProgram())。
program.setId(programRequestDto.getId())。
return programRepository.save(program)。
}
} catch (Exception e){
throw new UpdateDataException(e.getMessage())。
}
return null。
}
服務 程式Update(Long id)拋出UpdateDataException;
uj5u.com熱心網友回復:
改變服務方法的簽名以接受ProgramRequestDto,如下:
public Program Update(ProgramRequestDto programRequestDto) throws UpdateDataException
并相應地更新控制器:
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
final Long id,
final ProgramRequestDto programRequestDto) throws UpdateDataException {
ProgramResponseDto programResponseDto = ProgramMapper.INSTANCE.programToProgramResponseDTO(
programService.Update(programRequestDto))。
log.info(LocaleContext.format("response.success",
(new Object() {}.getClass().getEnclosingMethod().getName() )。
HttpStatus.OK.toString())。
return status(HttpStatus.OK).body(programResponseDto)。
不確定這是否是你想要的,但這是我從你的問題中理解的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309943.html
標籤:
