使用假客户端时,Spring服务返回内容为空

时间:2017-06-20 07:00:04

标签: java spring feign

创建了一个ui应用程序来测试我的服务。 UI与FooServiceUI有关系。 FooServiceUI使用feign客户端向BarServiceAccessor发送请求(在fooserviceui中作为接口实现)。但假装客户回报这样的反应;

{"datas": [PagedResource { content: [], metadata: Metadata { number: 0, total pages: 1, total elements: 3, size: 200 }, links: [] }]}

当我直接向BarService发送请求时,我可以看到所有这些数据。

FooService getAll方法;

@RequestMapping(method = RequestMethod.GET, path = "/api/datas/")
public ResponseEntity<String> getAllDatas()
{
    PagedResources<DataResource> responseEntity = null;

    try
    {
        responseEntity = dataManagementAccessor.getAll(0, 200);
    }
    catch (Exception e)
    {
        LOG.error("Exception " + e.toString());
    }
    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .body("{\"datas\": [" + responseEntity + "]}");
}

访问器;

@FeignClient("https://datamanagement")
public interface DataManagementAccessor{
@RequestMapping(value = "/api/datas/", method = GET)
    PagedResources<DataResource> getAll(@RequestParam("page") final Integer page,
        @RequestParam("size") final Integer size);
}

BarService代码看起来像;

@RequestMapping(method = GET)
@ResponseStatus(OK)
@ApiOperation(value = "Get all datas")
@ApiResponses(value = {@ApiResponse(code = SC_OK, message = "OK", response = DataPageResponse.class),
        @ApiResponse(code = SC_BAD_REQUEST, message = BAD_REQUEST_MESSAGE, response = String.class),
        @ApiResponse(code = SC_UNAUTHORIZED, message = UNAUTHORIZED_MESSAGE, response = String.class),
        @ApiResponse(code = SC_FORBIDDEN, message = FORBIDDEN_MESSAGE, response = String.class),
        @ApiResponse(code = SC_NOT_FOUND, message = NOT_FOUND_MESSAGE, response = String.class)})
    public PagedResources<Resource<DataResource>> getAll(@PageableDefault(sort = {"name"}) final Pageable pageable,
        final PagedResourcesAssembler<DataResource> pagedAssembler)
{
    final Page<DataData> allDatas = dataService.getAllDatas(pageable);

    final Page<DataResource> pagedResources = allDatas.map(
            d-> conversionService.convert(d, DataResource.class));
    pagedResources.forEach(resource -> controllerLinkHandler.addDataResourceLink(resource));
    return pagedAssembler.toResource(pagedResources);
}

我已经尝试将spring jpa添加到客户服务的gradle中,并尝试根据使用流api集合来更改映射,但仍然无法正常工作。

在使用_embedded行作为响应进行序列化时,也许某些内容无法正常工作?

1 个答案:

答案 0 :(得分:0)

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)

我忘了在main方法中添加这些行。