无法将类型为"java.lang.String"的属性值转换为属性"事务"所需的类型"java.util.List"



我正在使用thymelaf,并在将数据从String转换为List时遇到一些错误。我在这里附上了我的代码

我的实体类:

@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
@NotNull(message = "First Name cannot be empty")
@Size(min = 1, message = "First Name cannot be empty")
private String firstName;
@Column(name = "last_name")
@NotNull(message = "Last Name cannot be empty")
@Size(min = 1, message = "Last Name cannot be empty")
private String lastName;
@Column(name = "email")
@NotNull(message = "Email ID cannot be empty")
@Pattern(regexp = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+$",
message = "Enter valid mail id")
private String email;
@Column(name = "branch")
@NotNull(message = "Branch name cannot be empty")
private String branch;
@Column(name = "balance")
private double balance;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private List<Transaction> transactions;
public Customer(String firstName, String lastName, String email, String branch, double balance) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.branch = branch;
this.balance = balance;
}
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public List<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}
public void addTransaction(Transaction transaction){
if(transaction == null) {
transactions = new ArrayList<>();
}
transactions.add(transaction);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
", email='" + email + ''' +
", branch='" + branch + ''' +
", balance=" + balance +
'}';
}

@Entity
@Table(name = "transactions")
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "transaction")
private double amount;
public Transaction() {
}
public Transaction(double amount) {
this.amount = amount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Transaction{" +
"id=" + id +
", amount=" + amount +
'}';
}

控制器:

@Controller
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerRestService customerRestService;
public static String username;
public static Object password;
public CustomerController() {
}
@GetMapping("/list")
public String listCustomers(Model model, @CurrentSecurityContext(expression = "authentication")Authentication authentication) {
username = authentication.getName();
password = authentication.getCredentials();
List<Customer> customers = customerRestService.getCustomerList();
model.addAttribute("customers", customers);
return "list-customers";
}
@GetMapping("/showFormToAddCustomer")
public String showFormToAddCustomer(Model model) {
Customer customer = new Customer();
model.addAttribute("customer", customer);
return "customer-form";
}
@PostMapping("/saveCustomer")
public String saveCustomer(@ModelAttribute("customer") Customer customer) {
System.out.println("n" + customer);
System.out.println(customer.getTransactions());
customerRestService.saveCustomer(customer);
return "redirect:/customers/list";
}
@GetMapping("/showFormToUpdateCustomer")
public String showformForUpdate(@RequestParam("customerId") int id,
Model model) {
Customer customer = customerRestService.findCustomerById(id);
System.out.println(customer);
System.out.println(customer.getTransactions());

model.addAttribute("customer", customer);

return "customer-form";
}
}

服务:

@Service
public class CustomerRestServiceImple implements CustomerRestService{
private RestTemplate restTemplate;
private String restUrl;
@Autowired
public CustomerRestServiceImple(RestTemplate restTemplate,
@Value("${crm.rest.url}") String restUrl) {
this.restTemplate = restTemplate;
this.restUrl = restUrl;
}
private HttpHeaders httpHeaders() {
String username = CustomerController.username;
Object password = CustomerController.password;
String plainCreds = username+":"+password.toString();
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
//HttpEntity<String> request = new HttpEntity<String>(headers);
return headers;
}
@Override
public List<Customer> getCustomerList() {
HttpEntity<String> request = new HttpEntity<>(httpHeaders());
ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(restUrl, HttpMethod.GET, request,
new ParameterizedTypeReference<List<Customer>>() {});
List<Customer> customers = responseEntity.getBody();
return customers;
}
@Override
public Customer findCustomerById(int id) {
//Customer customer = restTemplate.getForObject(restUrl + "/" + id, Customer.class);
HttpEntity<String> request = new HttpEntity<>(httpHeaders());
ResponseEntity<Customer> responseEntity = restTemplate.exchange(restUrl + "/" + id, HttpMethod.GET, request, Customer.class);
Customer customer = responseEntity.getBody();
return customer;
}
@Override
public void saveCustomer(Customer customer) {
System.out.println("n" + customer);
System.out.println(customer.getTransactions());
HttpEntity<Customer> request = new HttpEntity<>(customer, httpHeaders());
int customerId = customer.getId();
if(customerId == 0){
restTemplate.exchange(restUrl, HttpMethod.POST, request, Customer.class);
} else {
restTemplate.exchange(restUrl, HttpMethod.PUT, request, Customer.class);
}
}
}

Html形式:

<h3>Customer Directory</h3>
<hr>
<p class="h4 mb-4">Save Customer</p>
<form action="#" th:action="@{/customers/saveCustomer}"
th:object="${customer}" method="POST">
<!-- Add hidden form field to handle the update -->
<input type="hidden" th:field="*{id}" />
<input type="text" th:field="*{firstName}"
class="form-control mb-4 col-4" placeholder="First Name">
<input type="text" th:field="*{lastName}"
class="form-control mb-4 col-4" placeholder="Last Name">
<input type="text" th:field="*{email}"
class="form-control mb-4 col-4" placeholder="Email">
<input type="text" th:field="*{branch}"
class="form-control mb-4 col-4" placeholder="Branch">
<input type="text" th:field="*{balance}"
class="form-control mb-4 col-4" placeholder="Balance">
<input type="text" th:field="*{transactions}"
class="form-control mb-4 col-4" placeholder="Transactions">
<!--input type="hidden" th:field="*{transactions}" /-->
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
<hr>
<a th:href="@{/customers/list}">Back to Customers List</a>
</div>

错误:

字段"transactions"上对象"customer"中的字段错误:拒绝值[[Transaction{id=1,amount=1000.0},Transaction{id=3,amount=100.00}]];codes[typeMismatch.customer.transactions,typeMismatch.transactions、typeMismatche.java.util.List,typeMismitch];arguments[org.springframework.context.support.DefaultMessageSourceResolvable:codes[customer.transactions,transactions];论点[];默认消息[交易]];默认消息[未能将类型为"java.lang.String"的属性值转换为属性"transactions"所需的类型"java.util.List";嵌套异常为org.springframework.core.covert.ConversionFailedException:未能从类型[java.lang.SString]转换为类型[@javax.persistence.OneToMany@javax.persistence.JoinColumn com.kaneki.springboot.bankapplication.entity.Transaction]value"[交易{id=1,amount=1000.0},交易{id=3,amount=100.00}]";嵌套异常是java.lang.NumberFormatException:对于输入字符串:";[交易{id=1,amount=1000.0},交易{id=3,amount=100.00}]"]

在HTML中,您将以字符串的形式接收Transaction字段,因此在控制器中,您必须处理该字符串并从中提取id和金额,然后将该id和金额设置为事务表。试试这个,如果你已经在控制器中尝试或编写了这个,请共享控制器的代码。

最新更新