java - 自定义 Controller 中的 Spring REST 响应不同

标签 java spring spring-data spring-data-rest spring-rest

我有几个自动创建 REST 端点的 Controller 。

@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 时:

@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();
    }
}

我会得到一个看起来与自动生成的 Controller 响应完全不同的响应:

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

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


编辑:找到这个:Enable HAL serialization in Spring Boot for custom controller method

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

最佳答案

正如@chrylis 建议的那样,您应该更换您的 @Controller带有 @RepositoryRestController 的注释让 spring-data-rest 调用它的 ResourceProcessors 来自定义给定的资源。

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

  • 您需要 Autowiring 此对象。

    @Autowired private PagedResourcesAssembler<Books> bookAssembler;

  • 你的返回声明应该是这样的

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

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

关于java - 自定义 Controller 中的 Spring REST 响应不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922466/

相关文章:

java - JsonMappingException : Cannot find a deserialzer for non-concrete Map type

java - Spring data rest 中自定义分页和排序的查询

java - 捕获异常的 Spring 事务

java - 在 Grails Spring Data 应用程序中混合 java 和 groovy 代码时出错

java - 非公共(public)构造函数导致 Tomcat7 出现问题?

java - Spring findBy 查询方法查找启用的国家以及包含某个城市的国家

java - spring @Value 无法工作

javascript - 如何从Java中的某个键中获取值?

Java 1.6 java.lang.IndexOutOfBoundsException 问题

java - Spring Boot JSP 配置不起作用