【问题标题】:Pass in entity link as @RequestParam to @RepositoryRestController method将实体链接作为@RequestParam 传递给@RepositoryRestController 方法
【发布时间】:2017-07-14 11:15:10
【问题描述】:

我有一个@RepositoryRestController

@RequestMapping(value = "/products/salesReport", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody
    ResponseEntity<?> get(
            @RequestParam(value = "customer", required = false) Resource<Organization> customer,
            @RequestParam(value = "supplier", required = false) Organization supplier,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @RequestParam(value = "startDate", required = false) Date startDate,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @RequestParam(value = "endDate", required = false) Date endDate,
            Pageable pageable
    ) { ...

这里的组织类型请求参数,我想作为 SDR 样式的 uris 传入。当我传入 Long id 时,它可以正常工作并自动转换为实体对象。但是当我传入uri时,我得到:

Failed to convert value of type [java.lang.String] to required type [org.springframework.hateoas.Resource]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.hateoas.Resource]: no matching editors or conversion strategy found"

有谁知道 Spring 是否真的支持自动将 uri 转换为 @RepositoryRestController 中的实体对象以及如何实现?

【问题讨论】:

    标签: spring spring-data-rest


    【解决方案1】:

    试试这个方法:

    @RepositoryRestController
    @RequestMapping("/products")
    public class ProductsController {
    
        @Autoware
        private ProductService productService;
    
        @GetMapping("/{id}/report/{data}")
        public ResponseEntity<?> getReport(@PathVariable("id") Product product, @PathVariable("date") Date date) {
            // some checks...
            return ResponseEntity.ok(productService.makeReport(product, date);
        }
    }
    

    【讨论】:

    • 是的,但我有 6-7 个参数,它们都是可选的(用于过滤)。所以这种方法并没有真正的帮助。
    • 另外,我将这些参数编码在 url 中而不是在正文中。
    猜你喜欢
    • 2015-11-02
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多