如何让Spring MVC + HATEOAS将资源列表编码为HAL?

Dmi*_*sky 8 java spring spring-mvc spring-hateoas

我有一个Spring HATEOAS Resource这样ModelResource extends Resource<Model>.

@RestController我要创建新的方法ModelS:

@RequestMapping(value = "", method = POST)
public ResponseEntity<ModelResource> postModel() {
    Model model = service.create().model;
    ModelResource resource = assembler.toResource(model);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(URI.create(resource.getLink("self").getHref()));

    return new ResponseEntity<>(resource, headers, CREATED);
}
Run Code Online (Sandbox Code Playgroud)

ModelResource从上面的方法返回的创建是HAL编码的:

$ curl -v -XPOST localhost:8080/models
> POST /models HTTP/1.1                                                                                                                                                       
> User-Agent: curl/7.32.0                                                                                                                                                     
> Host: localhost:8080                                                                                                                                                        
> Accept: */*                                                                                                                                                                 
>                                                                                                                                                                             
< HTTP/1.1 201 Created                                                                                                                                                        
< Date: Sun, 25 Jan 2015 11:51:50 GMT                                                                                                                                         
< Location: http://localhost:8080/models/0                                                                                                                                    
< Content-Type: application/hal+json; charset=UTF-8                                                                                                                           
< Transfer-Encoding: chunked
< Server: Jetty(9.2.4.v20141103)                                                                                                                                              
<                                                                                                                                                                             
{                                                                                                                                                                             
  "id" : 0,                                                                                                                                                                   
  "_links" : {                                                                                                                                                                
    "self" : {                                                                                                                                                                
      "href" : "http://localhost:8080/models/0"                                                                                                                               
    }                                                                                                                                                                        
  }                                                                                                                                                                           
}
Run Code Online (Sandbox Code Playgroud)

在同一个控制器中也有一个列出Models 的方法.

@RequestMapping(value = "", method = GET)
public List<ModelResource> getModels() {
    return service.find().stream()
        .map(modelProxy -> assembler.toResource(modelProxy.model))
        .collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,此方法返回纯JSON,而不是HAL:

$ curl -v localhost:8080/models                                                                                                                                               
> GET /models HTTP/1.1                                                                                                                                                        
> User-Agent: curl/7.32.0                                                                                                                                                     
> Host: localhost:8080                                                                                                                                                        
> Accept: */*                                                                                                                                                                 
>                                                                                                                                                                             
< HTTP/1.1 200 OK                                                                                                                                                             
< Date: Sun, 25 Jan 2015 11:52:00 GMT                                                                                                                                         
< Content-Type: application/json;charset=UTF-8                                                                                                                                
< Transfer-Encoding: chunked                                                                                                                                                  
< Server: Jetty(9.2.4.v20141103)                                                                                                                                              
<                                                                                                                                                                             
[ {                                                                                                                                                                           
  "id" : 0,                                                                                                                                                                   
  "links" : [ {                                                                                                                                                               
    "rel" : "self",                                                                                                                                                           
    "href" : "http://localhost:8080/models/0"                                                                                                                                 
  } ]                                                                                                                                                                         
} ]
Run Code Online (Sandbox Code Playgroud)
  • 为什么第一个方法返回HAL,第二个方法返回普通JSON?
  • 如何指定一致的行为?

我已经读过了@EnableHypermediaSupport,但我没有在我的代码中设置任何地方.

Dmi*_*sky 8

这个GitHub问题:

这按预期工作.HAL定义顶级资源必须是文档.因此,根据定义,普通列表不能是HAL文档.我们已经将HAL自定义限制为仅在要呈现的根对象是ResourceSupport或其子类型时应用,以防止任意对象应用HAL自定义.如果创建ResourceSupport而不是List,则应该看到正确的HAL文档呈现.

HAL入门提供了更多的细节,什么这样的顶级包装资源的样子例子.在Spring HATEOAS中,您Resources<T>用来表示这样一个包装器,它本身就是一个Resource<T>包含selfrel:

@RequestMapping(value = "", method = GET)                                                                                                                                     
public Resources<ModelResource> getModels() {
    List<ModelResource> models = service.find().stream()
        .map(modelVertex -> assembler.toResource(modelVertex.model))
        .collect(Collectors.toList());
    return new Resources<>(models, linkTo(methodOn(ModelController.class).getModels()).withRel("self"));
}
Run Code Online (Sandbox Code Playgroud)

Resources<T>然后将返回的类型编码为包含嵌入文档的文档:

$ curl -v localhost:8080/models                                                                                                                                               
> GET /models HTTP/1.1                                                                                                                                                        
> User-Agent: curl/7.32.0                                                                                                                                                     
> Host: localhost:8080                                                                                                                                                        
> Accept: */*                                                                                                                                                                 
>                                                                                                                                                                             
< HTTP/1.1 200 OK                                                                                                                                                             
< Date: Sun, 25 Jan 2015 13:53:47 GMT                                                                                                                                         
< Content-Type: application/hal+json; charset=UTF-8                                                                                                                           
< Transfer-Encoding: chunked                                                                                                                                                  
< Server: Jetty(9.2.4.v20141103)                                                                                                                                              
<                                                                                                                                                                             
{                                                                                                                                                                             
  "_links" : {                                                                                                                                                                
    "self" : {                                                                                                                                                                
      "href" : "http://localhost:8080/models"                                                                                                                                 
    }                                                                                                                                                                         
  },                                                                                                                                                                          
  "_embedded" : {                                                                                                                                                             
    "modelList" : [ {                                                                                                                                                         
      "id" : 0,                                                                                                                                                               
      "_links" : {                                                                                                                                                            
        "self" : {                                                                                                                                                            
          "href" : "http://localhost:8080/models/0"                                                                                                                           
        },                                                                                                                                                                    
        "links" : {                                                                                                                                                           
          "href" : "http://localhost:8080/models/0/links"                                                                                                                     
        }                                                                                                                                                                     
      }                                                                                                                                                                       
    } ]                                                                                                                                                                       
  }                                                                                                                                                                           
}
Run Code Online (Sandbox Code Playgroud)

如上所述,Resources<T>延伸ResourceSupport就像Resource<T>.因此,您可以创建一个ResourceAssemblerfor Resources<ModelResource>,以避免必须self手动创建链接,并通常封装Resource<ModelResource>创建.


这个答案表明,如果可用的话,Spring Boot会启用HAL渲染,这说明资源在可能的情况下呈现为HAL.