I'm trying to update my DTO with Reflection.
The problem is that some fields in my DTO are enums and I get an error that I can not set the enum field to String.
DTO:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Table(name = "xxx")
public class Model {
@Id
@Column(name = "id")
private String runId;
@Column(name = "name")
private String name;
@Column(name = "status")
@Enumerated(EnumType.STRING)
private ExecutionStatus status;
}
Controller:
@PatchMapping(path = "/{id}", consumes = "application/json")
public ResponseEntity<Void> partialUpdateModel(@PathVariable String id, @RequestBody Map<Object, Object> fields)
throws Exception {
Optional<Model> model= service.getById(id);
if (model.isPresent()) {
fields.forEach((key, value) -> {
Field field = ReflectionUtils.findField(Model.class, (String) key);
field.setAccessible(true);
ReflectionUtils.setField(field, model.get(), value);
});
So when it comes to the enum field, the field can not be set. It says
cannot set ExecutionStatus to String