0

我在 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 结果有content, pageable, 和其他一些字段。

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

4

0 回答 0