[英]Spring boot, thymeleaf @PostMapping 405 error message
I am new here and also new in java programing.我是新来的,也是 java 编程的新手。 I've been strugling second day now with @PostMapping in spring boot MVC.第二天,我一直在为 spring 引导 MVC 中的 @PostMapping 苦苦挣扎。 I can;t understand what's wrong with my code.我无法理解我的代码有什么问题。 I'm working on very simple bank account project.我正在从事非常简单的银行账户项目。
I can't find out whats wrong with connecting @PostMapping in AccountController in execute deposit method, and with thymeleaf form, which should add amount of deposit to account ballance.我无法找出在执行存款方法中连接 AccountController 中的 @PostMapping 以及 thymeleaf 表格有什么问题,这应该将存款金额添加到账户余额中。 I'm just started learning spring so please be gentle:)我刚开始学习 spring 所以请温柔一点:)
`@Controller
public class AccountController {
@Autowired
private AccountService accountService;
@Autowired
private AccountRepository accountRepository;
// display all accounts
@AliasFor(value = "accounts")
@GetMapping("/accounts")
public String showAccountsList(Model model) {
List<Account> listAccounts = accountService.listAll();
model.addAttribute("listAccounts", listAccounts);
return "accounts";
}
@Autowired
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
//get Account by Id
@GetMapping("accounts/{id}")
public ResponseEntity<Account> getAccountById(@PathVariable Long id) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
return ResponseEntity.ok(account);
}
@GetMapping("/account_operations/{id}")
public String getAccountDetailsById(@PathVariable Long id, Model model) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
model.addAttribute("account", account);
return "account_operations";
}
@GetMapping("/account_operations/deposit/{id}")
public String makeDeposit(@PathVariable Long id, Model model) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
model.addAttribute("account", account);
return "deposit";
}
@PostMapping(value = "/account_operations/deposit/{id}")
public String executeDeposit(@ModelAttribute("amount") double amount, Account account, RedirectAttributes ra) {
accountService.executeDeposit(account, amount);
ra.addFlashAttribute("message", "The customer has been saved successfully.");
return "redirect:/customer_accounts";
}`
`<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Deposit</title>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css"/>
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid text-center">
<div><h2>Deposit</h2></div>
<div class="m-3">
<a class="h4" th:href="@{/accounts}">Back to accounts</a>
<p>test</p>
</div>
<div>
<div th:if="${account}">
<h2>Deposit to account:</h2>
<h3>[[${account.accountNumber}]]</h3>
<p>[[${account.balance}]]</p>
<p>[[${account.accountType}]]</p>
</div>
<div>
<h2>Amount to deposit</h2>
<form th:action="@{/account_operations/deposit}" method="post" th:object="${account}" style="max-width: 500px; margin: 0 auto; font-size: small">
<div class="mb-3">
<label for="amount" class="form-label">Amount</label>
<input type="number" th:value="${amount}" th:field="*{amount}" name="amount" id="amount" class="form-control form-control-sm" placeholder="Deposit Amount" required>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary m-2">Deposit</button>
<button type="button" class="btn btn-secondary m-2" onclick="cancelForm()">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>`
`@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
@EntityListeners(AuditingEntityListener.class)
@SecondaryTable(name = "customer", pkJoinColumns = @PrimaryKeyJoinColumn(name = ""))
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private double balance;
private double interest;
private String accountNumber;
private AccountType accountType;
private double amount;
@ManyToOne
@JoinColumn(name = "customer_id", insertable = false, updatable = false)
@JsonBackReference
private Customer customer;
public Account(double balance, double interest, AccountType accountType) {
this.balance = balance;
this.interest = interest;
this.accountNumber = generateIbanAccountNumber();
this.accountType = accountType;
}
public String generateIbanAccountNumber() {
return new Iban.Builder().countryCode(CountryCode.PL).bankCode("696").buildRandom().toString();
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}`
You should have csrf token in your form.您的表单中应该有 csrf 令牌。 Put below code before form closing tag.将下面的代码放在表单结束标记之前。
<input
type="hidden"
th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.