Java 具有hal响应的Spring boot始终定义结构

Java 具有hal响应的Spring boot始终定义结构,java,json,rest,spring-boot,hal,Java,Json,Rest,Spring Boot,Hal,我有一个spring引导应用程序,它使用PagedResources和HAL返回一组资源 这是一个常见响应的示例: { "_embedded": { "mappings": [ { "formId": "857353204373673", "formName": null, ..... } ] }, "_links": {.....}, "page": {.....} } 否则,如果要返回的

我有一个spring引导应用程序,它使用PagedResources和HAL返回一组资源

这是一个常见响应的示例:

{
  "_embedded": {
    "mappings": [
      {
        "formId": "857353204373673",
        "formName": null,
        .....
      }
    ]
  },
  "_links": {.....},
  "page": {.....}
}
否则,如果要返回的集合为null(可能为空),则响应将为:

{
  "_links": {.....},
  "page": {.....}
}
我希望有一个总是返回JSON的响应,其中包含_嵌入式节点及其所有字段(显然为空),如:

我的选择是始终让客户机看到我的JSON的整个结构(也许这不是最佳实践?)

下面是代码片段:

@RequestMapping(
        method = RequestMethod.GET,
        path = "/mappings")
@ResponseStatus(HttpStatus.OK)
@ApiImplicitParams({
        @ApiImplicitParam(name = "page", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "size", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "sort", dataType = "string",  paramType = "query", allowMultiple = true),
        @ApiImplicitParam(name = RestConfiguration.HEADER_ACCEPT_LANGUAGE, dataType = "string",  paramType = "header", defaultValue = RestConfiguration.DEFAULT_LOCALE_TAG)
})
public PagedResources<MappingResource> getMappings(@ApiParam(required = true) @PathVariable final Integer customerId,
                                                   @ApiIgnore @PageableDefault final Pageable pageable,
                                                   HttpServletRequest request) {

    ApplicationResourcesValidator.validateCustomerId(customerId);
    if (customerRepository.findOne(customerId) == null) {
        throw new CustomerNotFoundException(customerId);
    }

    customerIdProvider.set(customerId);

    Page<UserdbFormMapping> mappings = userdbFormMappingRepository.findAllIfUserdbValid(pageable);

    MappingResourceAssembler mappingResourceAssembler = new MappingResourceAssembler(
            MappingController.class,
            MappingResource.class,
            customerId,
            facebookApiClient,
            userdbRepository,
            userdbFormMappingRepository,
            userdbFormMappingFieldRepository,
            userdbFormSubscriptionLogCacheRepository,
            persistenceManager);

    LOGGER.info("serving " + request.getRequestURI());

    return mappingPagedResourceAssembler.toResource(mappings, mappingResourceAssembler);
}
@RequestMapping(
method=RequestMethod.GET,
path=“/mappings”)
@ResponseStatus(HttpStatus.OK)
@ApitParams({
@apimplicitparam(name=“page”,dataType=“int”,paramType=“query”),
@apimplicitparam(name=“size”,dataType=“int”,paramType=“query”),
@apimplicitparam(name=“sort”,dataType=“string”,paramType=“query”,allowMultiple=true),
@apimplicitparam(name=RestConfiguration.HEADER\u ACCEPT\u语言,dataType=“string”,paramType=“HEADER”,defaultValue=RestConfiguration.DEFAULT\u LOCALE\u标记)
})
public PagedResources getMappings(@ApiParam(required=true)@PathVariable最终整数customerId,
@APIGNORE@PageableDefault最终可分页可分页,
HttpServletRequest(请求){
ApplicationResourcesValidator.validateCustomerId(customerId);
if(customerRepository.findOne(customerId)==null){
抛出新CustomerNotFoundException(customerId);
}
customerIdProvider.set(customerId);
页面映射=userdbFormMappingRepository.findAllIfUserdbValid(可分页);
MappingResourceAssembler MappingResourceAssembler=新MappingResourceAssembler(
MappingController.class,
MappingResource.class,
客户ID,
facebookApiClient,
userdbRepository,
userdbFormMappingRepository,
userdbFormMappingFieldRepository,
userdbFormSubscriptionLogCacheRepository,
坚持管理者);
info(“服务”+request.getRequestURI());
返回mappingPagedResourceAssembler.toResource(映射,mappingResourceAssembler);
}
任何建议都将不胜感激

提前感谢,,
安德里亚。

你能澄清一下吗?你的意思是说,你现在想知道当集合返回null或null时是否显示集合结构?是的,我想知道即使要返回的集合为空和/或null,如何始终显示_embeddedhal属性。你的愿望是对的。在客户端解析响应时,它肯定会更好。也就是说,您很可能可以通过不抛出“customer not found异常”或更改捕获异常的逻辑来做到这一点。关于后者,我在上面的代码中没有看到这种情况。使用Spring Boot
1.4.0.RELEASE
,您可以使用
@JsonComponent
注册自定义序列化程序。
@RequestMapping(
        method = RequestMethod.GET,
        path = "/mappings")
@ResponseStatus(HttpStatus.OK)
@ApiImplicitParams({
        @ApiImplicitParam(name = "page", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "size", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "sort", dataType = "string",  paramType = "query", allowMultiple = true),
        @ApiImplicitParam(name = RestConfiguration.HEADER_ACCEPT_LANGUAGE, dataType = "string",  paramType = "header", defaultValue = RestConfiguration.DEFAULT_LOCALE_TAG)
})
public PagedResources<MappingResource> getMappings(@ApiParam(required = true) @PathVariable final Integer customerId,
                                                   @ApiIgnore @PageableDefault final Pageable pageable,
                                                   HttpServletRequest request) {

    ApplicationResourcesValidator.validateCustomerId(customerId);
    if (customerRepository.findOne(customerId) == null) {
        throw new CustomerNotFoundException(customerId);
    }

    customerIdProvider.set(customerId);

    Page<UserdbFormMapping> mappings = userdbFormMappingRepository.findAllIfUserdbValid(pageable);

    MappingResourceAssembler mappingResourceAssembler = new MappingResourceAssembler(
            MappingController.class,
            MappingResource.class,
            customerId,
            facebookApiClient,
            userdbRepository,
            userdbFormMappingRepository,
            userdbFormMappingFieldRepository,
            userdbFormSubscriptionLogCacheRepository,
            persistenceManager);

    LOGGER.info("serving " + request.getRequestURI());

    return mappingPagedResourceAssembler.toResource(mappings, mappingResourceAssembler);
}