在没有存储库的情况下使用CRNK

问题描述 投票:0回答:1

但是,我们已经将JSON:API用于我们的REST端点进行了标准化;并非我们的所有数据都围绕存储库,并且CRNK似乎需要存储库才能正常工作。

正确吗?

示例

我写了一个非常简单的Spring Boot 2.1.9示例,它具有单个控制器并在其中包含CRNK,但是当我进入控制器时,没有得到预期的JSON:API输出。

请记住,我只是开始研究CRNK,这只是我正在测试的一个简单的“ hello world”类型的应用程序]]

这是我的例子

package com.example.crnkdemo;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test/v1.0")
public class Controller {
    @GetMapping(value = "/{country}", produces = "application/vnd.api+json")
    public Country test1(@PathVariable String country, @RequestParam(name = "filter[region]", required = false) String filter) {
        return new Country(country, filter);
    }
}

国家只是我创建的一个虚拟类,它是:

package com.example.crnkdemo;

import io.crnk.core.resource.annotations.JsonApiId;
import io.crnk.core.resource.annotations.JsonApiResource;

@JsonApiResource(type = "country")
@AllArgsConstructor
@Data
public class Country {

    @JsonApiId
    private String country;
    private String region;

结果

但是当我使用以下URL http://localhost:8080/test/v1.0/US?filter[region]=northeast

时,我得到
{
    "country": "US",
    "region":"northeast"
}

我希望得到结果的JSON API类型

{
  "data": {
    "type": "country",
    "id": "US",
    "attributes": {
      "region": "northeast"
    }
}

谢谢!

但是,我们已经将JSON:API用于我们的REST端点进行了标准化;并非我们的所有数据都围绕存储库,并且CRNK似乎需要存储库才能正常工作。那是对的吗? ...

spring-boot json-api crnk
1个回答
0
投票

我遇到了类似的问题,问题是我的依赖项中存在io.crnk:crnk-format-plain-json(仅从示例应用程序复制),这改变了响应的外观(非JSON-API)。因此,首先查看您的Maven / gradle配置。

© www.soinside.com 2019 - 2024. All rights reserved.