Я использую spring-thymeleaf, чтобы вернуть страницу с объектом в качестве атрибута spring. А затем сделать AJAX POST-запрос к контроллеру пружины. Некоторые, как я получаю
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
ошибка, что странно, я не делаю GET-запрос.
Это первый контроллер пружины, затем устанавливающий объект Param и передаваемый в представление.
@RequestMapping("/listing")
public String privateReport(Principal principal, HttpServletRequest request, Model model) throws Exception {
Param param = new Param();
param.setStartDate(startDate);
param.setEndDate(endDate);
model.addAttribute("params", param);
return "listing-report";
}
Представление, которое включает в себя таблицу данных и делает запись AJAX для получения данных. путем передачи Param в виде json отчета о листинге объекта. html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>Private Corporate Patient Listing Report</h2>
</div>
<div class="container" style="margin-top: 25px;">
<table id="casesTable" class="table table-striped table-bordered nowrap">
<thead>
<tr>
<th>Date</th>
<th>Case Number</th>
<th>Patient Name</th>
<th>Address</th>
<th>Status</th>
<th>Charge Amt</th>
<th>Clinic</th>
<th>Doctor</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
crossorigin="anonymous"></script>
<script th:inline="javascript">
var params = [[${params}]];
$('#reportsTable').DataTable({
"processing": true,
"serverSide": true,
"bLengthChange": false,
"bFilter": false,
"ajax": {
type : "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url : "/report/listing-data",
data: JSON.stringify(params),
dataSrc: ''
},
"columns": [
{"data": "caseStartDate"},
{"data": "caseNumber"},
{"data": "patientName"},
{"data": "address"}
]
});
</script>
</body>
</html>
Пружинный REST-контроллер, который возвращает данные:
@RequestMapping(value = "/report/listing-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity privateReportData(@RequestParam("page") int page,
@RequestParam("size") int size,
@RequestBody Param param) {
param.setPage(page);
param.setSize(size);
logger.info("Requesting table data " + param);
return ResponseEntity.ok(reportDataService.getView(param));
}
Модель Param:
@Getter
@Setter
@ToString
public class Param extends CommonParams {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
}
Модель CommonParams:
@Getter
@Setter
@ToString
public class CommonParams {
private int page;
private int size;
}
Где бы я мог ошибаться? Заранее спасибо