小编jos*_*e_p的帖子

如何删除Spring HATEOAS中的"_embedded"属性

我正在使用Spring Boot和HATEOAS来构建REST API,当我的API返回一个集合时,它被包装在一个"_embedded"属性中,如下所示:

{
   "_links":{
      "self":{
         "href":"http://localhost:8080/technologies"
      }
   },
   "_embedded":{
      "technologies":[
         {
            "id":1,
            "description":"A",
            "_links":{
               "self":{
                  "href":"http://localhost:8080/technologies/1"
               }
            }
         },
         {
            "id":2,
            "description":"B",
            "_links":{
               "self":{
                  "href":"http://localhost:8080/technologies/2"
               }
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

我希望响应是这样的:

{
   "_links":{
      "self":{
         "href":"http://localhost:8080/technologies"
      }
   },
   "technologies":[
      {
         "id":1,
         "description":"A",
         "_links":{
            "self":{
               "href":"http://localhost:8080/technologies/1"
            }
         }
      },
      {
         "id":2,
         "description":"B",
         "_links":{
            "self":{
               "href":"http://localhost:8080/technologies/2"
            }
         }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我的技术控制器:

@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologiesController {
    ...
    @ResquestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
    public Resources<Resource<Technology>> getAllTechnologies() …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-hateoas spring-boot

17
推荐指数
3
解决办法
1万
查看次数

如何建立模板链接?

我有一个带有请求参数的方法,并且正在尝试从另一个资源链接到此方法。我希望链接是这样的:

"rel":{
  "href":".../resources{?param}",
  "templated":true     
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下操作但未成功:

//First attempt
resources.add(linkTo(methodOn(Controller.class).method(null)).withRel("rel")       
//Second attempt
resources.add(linkTo(methodOn(Controller.class).method("{parameter}")).withRel("rel")
//Third attempt
resources.add(entityLinks.linkToCollectionResource(LinkedResource.class).withRel("rel");
Run Code Online (Sandbox Code Playgroud)

spring-hateoas

5
推荐指数
1
解决办法
3121
查看次数

标签 统计

spring-hateoas ×2

rest ×1

spring ×1

spring-boot ×1