Spring 自定义控制器中的 REST 响应不同

Spring REST response is different in a custom controller

我有几个自动创建 REST 端点的控制器。

@RepositoryRestResource(collectionResourceRel = "books", path = "books")
public interface BooksRepository extends CrudRepository<Books, Integer> {
    public Page<Books> findTopByNameOrderByFilenameDesc(String name);
}

当我访问:http://localhost:8080/Books

我回来了:

{
    "_embedded": {
        "Books": [{
            "id": ,
            "filename": "Test123",
            "name": "test123",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/books/123"
                },
                "Books": {
                    "href": "http://localhost:8080/books/123"
                }
            }
        }]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/books"
        },
        "profile": {
            "href": "http://localhost:8080/profile/books"
        },
        "search": {
            "href": "http://localhost:8080/books/search"
        },
        "page": {
            "size": 20,
            "totalElements": 81,
            "totalPages": 5,
            "number": 0
        }
    }
}

当我创建自己的控制器时:

@Controller
@RequestMapping(value = "/CustomBooks")
public class CustomBooksController {
    @Autowired
    public CustomBookService customBookService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Book> search(@RequestParam(value = "q", required = false) String query,
                                 @PageableDefault(page = 0, size = 20) Pageable pageable) {
        return customBookService.findAll();
    }
}

我会收到一个与自动生成的控制器响应完全不同的响应:

{
    "content": [{
        "filename": "Test123",
        "name" : "test123"
    }],
    "totalPages": 5,
    "totalElements": 81,
    "size": 20,
    "number": 0,
}

我需要做什么才能让我的回复看起来像自动生成的回复?我想保持一致,这样我就不必为不同的响应重写代码。我应该换一种方式吗?


编辑: 发现:

但我不明白我需要在我的 REST 控制器中更改什么才能启用:PersistentEntityResourceAssembler。我在 Google 上搜索了 PersistentEntityResourceAssembler,但它总是让我返回类似的页面,但没有太多示例(或者该示例似乎对我不起作用)。

正如@chrylis 建议的那样,您应该将 @Controller 注释替换为 @RepositoryRestController,以便 spring-data-rest 调用它的 ResourceProcessors 来自定义给定的资源。

为了让您的资源遵循 HATEOAS 规范(例如您的 spring-data-rest BooksRepository),您的方法声明 return 类型应该类似于 HttpEntity<PagedResources<Resource<Books>>> 将 Page 对象转换为 PagedResources:

  • 您需要自动装配此对象。

    @Autowired private PagedResourcesAssembler<Books> bookAssembler;

  • 你的 return 语句应该像

    return new ResponseEntity<>(bookAssembler.toResource(customBookService.findAll()), HttpStatus.OK);

这些更改应该可以帮助您获得包含 "_embedded""_links" 属性的 org.springframework.hateoas.Resources 合规响应。