I have this problem now and I would like to ask for help, I can't acess te value of dto.getIdCliente()
, but this is passed like JSON for the ServicoPrestadoDTO dto
.
Codes bellow...
ServicoPrestadoController.java:
@RestController
@RequestMapping("/api/servicos-prestados")
@RequiredArgsConstructor
public class ServicoPrestadoController {
private final ClienteRepository clienteRepository;
private final ServicoPrestadoRepository servicoPrestadoRepository;
private final BigDecimalConverter bigDecimalConverter;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ServicoPrestado salvar(@RequestBody ServicoPrestadoDTO dto){
LocalDate data = LocalDate.parse(dto.getData(), DateTimeFormatter.ofPattern("dd/MM/yyyy"));
Integer idCliente = dto.getIdCLiente();
Cliente cliente =
clienteRepository.findById(idCliente)
.orElseThrow(() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cliente n?o encontrado!"));
ServicoPrestado servicoPrestado = new ServicoPrestado();
servicoPrestado.setDescricao(dto.getDescricao());
servicoPrestado.setData(data);
servicoPrestado.setCliente(cliente);
servicoPrestado.setValor(bigDecimalConverter.converter(dto.getPreco()));
return servicoPrestado;
}
@GetMapping
public List<ServicoPrestado> pesquisar(
@RequestParam(value = "nome", required = false) String nome,
@RequestParam(value = "mes", required = false) Integer mes
){
return servicoPrestadoRepository.findByNomeClienteAndMes("%" + nome + "%", mes);
}
}
ServicoPrestadoDTO.java:
@Data
@NoArgsConstructor
public class ServicoPrestadoDTO {
private String descricao;
private String preco;
private String data;
private Integer idCLiente;
}
ServicoPrestado.java:
@Entity
@Data
public class ServicoPrestado {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, length = 150)
private String descricao;
@ManyToOne
@JoinColumn(name = "id_cliente")
private Cliente cliente;
@Column
private BigDecimal valor;
@Column
@JsonFormat(pattern = "dd/MM/yyyy")
private LocalDate data;
}
I don't undertand why is not getting the servico
send by my servico-prestado-form.ts
like JSON.
question from:
https://stackoverflow.com/questions/65834370/internal-server-error-the-given-id-must-not-be-null 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…