Не отображает представление в случае наличия ошибок в форме Spring Boot
При отправкe формы с ошибками, получаю 500 ошибку, вместо корректного отображения страницы представления с ошибками. Подскажите как это можно исправить, либо в каком направлении искать ошибку.
@GetMapping
public String showCurrency(Model model) {
List<Currency> currencies = new ArrayList<>();
currencyRepository.findAll().forEach(i -> currencies.add(i));
model.addAttribute("currencies",currencies);
model.addAttribute("ConvertedCurrency", new ConvertedCurrency());
return "convert";
}
@PostMapping
public String showConvertationResult(@Valid ConvertedCurrency convertedCurrency, Errors errors) {
if (errors.hasErrors()) {
return "convert";
}
Optional<Currency> initCurrency = currencyRepository.findById(convertedCurrency.getInitCurrency());
Optional<Currency> targetCurrency = currencyRepository.findById(convertedCurrency.getTargetCurrency());
double amountAfterExchange = exchangeCurrency(convertedCurrency);
convertedCurrency.setAmountAfterExchange(amountAfterExchange);
convertedCurrency.setInitCurrency(initCurrency.get().getName());
convertedCurrency.setTargetCurrency(targetCurrency.get().getName());
valueTo = convertedCurrency;
convertedCurrencyRepository.save(convertedCurrency);
return "redirect:/convert/result";
HTML - форма.
<form method = "POST" th:object="${ConvertedCurrency}">
<div th:if="${#fields.hasErrors()}">
<span class="validationError">
Please correct the problems below and resubmit.
</span>
</div>
<label>
<select class = "CurrencyConverter" id = "0" name = "initCurrency">
<div th:each="currency : ${currencies}">
<option th:value = "${currency.id}" th:text="${currency.name}"></option>
</div>
</select>
<input type = "text" th:field="*{amountBeforeExchange}">
<span class="validationError"
th:if="${#fields.hasErrors('amountBeforeExchange')}"
th:errors="*{amountBeforeExchange}">error</span>
</label>
<br/>
<label>
<select class = "CurrencyConverter" id = "1" name = "targetCurrency">
<div th:each="currency : ${currencies}">
<option th:value = "${currency.id}" th:text="${currency.name}"></option>
</div>
</select>
<input type = "hidden" th:field="*{amountAfterExchange}">
</label>
<p><input type="submit" value="Конвертировать"></p>
</form>
<form action="/history">
<button>История конвертаций</button>
</form>
Класс сущность
@Data
@RequiredArgsConstructor
@Entity
@Table(name="converted_currency")
public class ConvertedCurrency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Pattern(regexp="^[0-9]*[.,]?[0-9]+$", message="Введите корректную сумму")
private String amountBeforeExchange;
private double amountAfterExchange;
private String initCurrency;
private String targetCurrency;
private Date date;
@PrePersist
void createdAt() {
this.date = new Date();
}
}