无法将 String 类型的值转换为属性“person”所需的类型 PersonDTO

问题描述 投票:0回答:1

我有一个名为 Tabel 的类,其中有一个人。 Tabel 类和 Person 类具有一对多关系。

@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "tabel")
public class Tabel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;
    private String comments;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id")
    private Person person;

}

@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor

public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String lastname;
    private String surname;

}

还有DTO

@Data
@Builder
@EqualsAndHashCode
@Jacksonized
public class TabelDto implements Serializable {
    private Long id;
    private String comments;
    private PersonDto person;
}

@Value
@Builder
public class PersonDto implements Serializable {
    Long id;
    String name;
    String lastname;
    String surname;
}

还有我的控制器

@PostMapping("/addTabel")
public String addTabel(@ModelAttribute TabelDto tabelDto) {
    tabelService.save(tabelDto);
    return "redirect:/addTabel";
}

@GetMapping("/addTabel")
public String addTabel(Model model) {
    TabelDto tabelDto = TabelDto.builder().build();
    model.addAttribute("persons", personService.findAllPerson());
    model.addAttribute("tabelDTO", tabelDto);
    return "addTabel";
}

我的 HTML Thymeleaf

<form action="#" th:action="@{/tabel/addTabel}" method="post" th:object="${tabelDTO}">
    <label>person:</label>
    <label>
        <select th:field="*{person}" class="form-control">
            <option th:each="person: ${persons}" th:value="${person.id}" th:text="${person.name}"></option>
        </select>
    </label>
    <button type="submit">Register</button>
</form>

当我尝试发送 POST 时,出现错误。

[tabel] [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public java.lang.String by.gus.tabel.controller.TabelController.addTabel(by.gus.tabel.dto.TabelDto): [Field error in object 'tabelDto' on field 'person': rejected value [1]; codes [typeMismatch.tabelDto.person,typeMismatch.person,typeMismatch.by.gus.tabel.dto.PersonDto,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tabelDto.person,person]; arguments []; default message [person]]; default message [Failed to convert value of type 'java.lang.String' to required type 'by.gus.tabel.dto.PersonDto'; Cannot convert value of type 'java.lang.String' to required type 'by.gus.tabel.dto.PersonDto' for property 'person': no matching editors or conversion strategy found]] ]

但是如果你放置 Table 类而不是 TableDTO

public String addTabel(@ModelAttribute Tabel tabelDto)

然后一切开始工作,但我想通过 DTO 进行工作,而不是直接与实体合作。

我该如何解决这个问题?

java spring-boot thymeleaf
1个回答
0
投票

我在这里看到的错误是您正在添加 tabel 并且您确实将 PersonDto 作为 tabel 内的人员。但是,您正在传递从选项中选择人员姓名时选择的人员 ID 值。

如果您只是尝试将 person.Id 作为 person 的值传递,您可以将变量设置为 Long 或 numeric。否则,您可以添加所有内容。

    @Data
    @Builder
    @EqualsAndHashCode
    @Jacksonized
    public class TabelDto implements Serializable {
        private Long id;
        private String comments;
        private Long personId; // Change from PersonDto to Long
    }

<form action="#" th:action="@{/tabel/addTabel}" method="post" th:object="${tabelDTO}">
    <label>person:</label>
    <label>
        <select th:field="*{personId}" class="form-control">
            <option th:each="person: ${persons}" th:value="${person.id}" th:text="${person.name}"></option>
        </select>
    </label>
    <button type="submit">Register</button>
</form>

<form action="#" th:action="@{/tabel/addTabel}" method="post" th:object="${tabelDTO}">
    <label>Comments:</label>
    <textarea th:field="*{comments}" class="form-control"></textarea>

    <label>Person Name:</label>
    <input th:field="*{person.name}" class="form-control" />

    <label>Person Lastname:</label>
    <input th:field="*{person.lastname}" class="form-control" />

    <label>Person Surname:</label>
    <input th:field="*{person.surname}" class="form-control" />

    <button type="submit">Register</button>
</form>
© www.soinside.com 2019 - 2024. All rights reserved.