我在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的下一个和上一个链接。