实际请求参数未满足消息参数条件“拒绝”:reason = {}

时间:2019-06-12 13:09:32

标签: java spring spring-boot httprequest thymeleaf

我的Thymeleaf模板和/或控制器有问题。

模板如下:

<div align="left">
    <form action="#" th:action="@{/rejection/{invoiceId}(invoiceId=${invoiceId})}" th:object="${rejection}" method="post">
        <ul class="form-style-1">
            <li>
                <label>Powód odrzucenia:<span class="required">*</span></label>
                <input type="text" th:field="*{reason}" id="reason" required>
            </li>
            <li>
                <input type="submit" value="Wyślij powód odrzucenia protokołu"/>
            </li>
        </ul>
    </form>
</div>

以下是控制器方法:

@GetMapping(params = "invoiceId", path = "/reject")
public String reject(@RequestParam("invoiceId") String invoiceId, @CurrentUser Contractor contractor, Model model) {
    InvoiceData invoiceData = invoiceDataRepository.findById(invoiceId).get();
    invoiceData.setStatus(StatusEnum.REJECTED.toString());
    invoiceDataRepository.save(invoiceData);
    Rejection rejection = new Rejection(invoiceId, null);
    model.addAttribute("contractor", contractor);
    model.addAttribute("rejection", rejection);
    return "reject";
}

@PostMapping(params = "rejection", path = "/rejection")
public String rejection(@RequestParam("rejection") Rejection rejection, @CurrentUser Contractor contractor, Model model) {
    InvoiceData invoiceData = invoiceDataRepository.findById(rejection.getInvoiceId()).get();
    model.addAttribute("contractor", contractor);
    model.addAttribute("rejection", rejection);
    emailService.sendRejectionMessage(contractor.getEmail(), invoiceData, rejection.getReason());
    logger.info("Invoice number "+ invoiceData.getInvoiceNumber() +" with ID " + invoiceData.getId() + " rejected for Contractor with ID " + contractor.getId());
    return "index";
}

当我尝试从拒绝模板发送数据时,出现错误:

2019-06-12 15:04:13.451  WARN 23678 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.UnsatisfiedServletRequestParameterException: Parameter conditions "rejection" not met for actual request parameters: reason={some text here}]

这是拒绝类:

@Data
@AllArgsConstructor
public class Rejection {

    private String invoiceId;
    private String reason;

}

我在做什么错?请求参数是Rejection对象,而不仅仅是Rejection字段reason ...

1 个答案:

答案 0 :(得分:0)

看到该错误的原因是params = "rejection"中有@PostMapping。期望使用名称为rejection的参数,但您没有提供任何参数。除非有特殊原因,否则请删除它。