Spring Boot-将Next和Prev链接添加到控制器响应

时间:2020-08-13 08:23:25

标签: spring spring-boot rest pageable

我在Spring Boot应用程序中有一个简单的控制器

@GetMapping("/")
public Page<StockDto> listStocks(Pageable pageable) {
    return stockService.list(pageable);
}

它调用list中的StockService方法以从数据库中获取项目列表。 StockRepository是一个使用@Repository注释扩展JpaRepository的类。

@Service
public class StockService {

    private StockRepository stockRepository;
    private ModelMapper modelMapper;

    @Autowired
    public StockService(StockRepository stockRepository, ModelMapper modelMapper) {
        this.stockRepository = stockRepository;
        this.modelMapper = modelMapper;
    }

    public Page<StockDto> list(Pageable pageable) {
        Page<StockEntity> stockEntities = stockRepository.findAll(pageable);

        return stockEntities.map(stockEntity -> modelMapper.map(stockEntity, StockDto.class));
    }
}

调用API的JSON结果包含contentpageable和其他一些字段。

我也没有使用Spring Data Rest。现在,我想添加两个Next和Prev字符串,它们具有获取资源以响应JSON的下一个和上一个链接。

0 个答案:

没有答案