【问题标题】:How to limit the size of a Key in a Json response in spring boot?如何限制 Spring Boot 中 Json 响应中 Key 的大小?
【发布时间】:2022-11-14 21:05:17
【问题描述】:

image 这是我的 JSON 响应,响应中有一个属性(键),名为数据这是列表列表,其中包含 1000 多个列表。

我想将两个不同控制器的数据大小(json 内的属性)限制为 30 个列表和 90 个列表。我不知道该怎么做。

【问题讨论】:

  • 您将需要以编程方式(在您的 java 实现中)准确说明如何为您的 DTO(数据传输对象)仅选取 30 个元素(或 90 个)

标签: java spring-boot


【解决方案1】:

将业务逻辑提取到 @Service 类,然后将电流限制作为参数从控制器传递

@Service
public class MyService {

  String createResponse(int limit) {
  //....
  List limitedData = data.sublist(0, limit);
  //...
  }
}


@Controller
public class MyController1 {

  @Autowired
  MyService myService

  String createResponse() {
    myService.createResponse(30);
  }
}



@Controller
public class MyController2 {

  @Autowired
  MyService myService

  String createResponse() {
    myService.createResponse(100);
  }
}

【讨论】:

    猜你喜欢
    • 2018-07-13
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2021-02-20
    • 2019-04-10
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多