如何在我的服务@PutMapping 中获取价值请求 DTO
How can take value request DTOs in my service @PutMapping
我正在尝试做一个@PutMapping,我会在其中放置所有逻辑来更改服务内的属性,但我 运行 遇到了问题。我如何从我的 requestDTO 中获取值以在 class 中播放并在服务中而不是在控制器中进行这些更改?
请求数据与响应相同class
private Long id;
private String program;
我的控制器和服务实现
@PutMapping(value = "/edit-program/test/{id}")
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
@PathVariable(value = "id") final Long id,
@Valid @RequestBody 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);
}
@Override
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;
}
服务
程序更新(长 id)抛出 UpdateDataException;
更改服务方法的签名以接受ProgramRequestDto
,如下所示:
public Program Update(ProgramRequestDto programRequestDto) throws UpdateDataException
并相应地更新控制器:
@PutMapping(value = "/edit-program/test/{id}")
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
@PathVariable(value = "id") final Long id,
@Valid @RequestBody 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);
}
不确定这是否是您想要的,但这是我从您的问题中了解到的。
我正在尝试做一个@PutMapping,我会在其中放置所有逻辑来更改服务内的属性,但我 运行 遇到了问题。我如何从我的 requestDTO 中获取值以在 class 中播放并在服务中而不是在控制器中进行这些更改?
请求数据与响应相同class
private Long id;
private String program;
我的控制器和服务实现
@PutMapping(value = "/edit-program/test/{id}")
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
@PathVariable(value = "id") final Long id,
@Valid @RequestBody 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);
}
@Override
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;
}
服务 程序更新(长 id)抛出 UpdateDataException;
更改服务方法的签名以接受ProgramRequestDto
,如下所示:
public Program Update(ProgramRequestDto programRequestDto) throws UpdateDataException
并相应地更新控制器:
@PutMapping(value = "/edit-program/test/{id}")
public ResponseEntity<ProgramResponseDto> updateProgramByIdTest (
@PathVariable(value = "id") final Long id,
@Valid @RequestBody 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);
}
不确定这是否是您想要的,但这是我从您的问题中了解到的。