Home > database >  How to limit the size of a Key in a Json response in spring boot?
How to limit the size of a Key in a Json response in spring boot?

Time:11-14

image This is my JSON repsonse, there is an attribute (key) in the response named as DATA which is list of lists and have 1000 list in it.

I want to limit the size of data(attribute inside json) to 30 lists and 90 lists for two different controllers. I am not able to find out how to do it.

CodePudding user response:

Extract business logic to a @Service class, and provide the required limit as a parameter from a Controller:

@Service
public class MyService {

  MyDto createResponse(int limit) {
    //.... slice list size, e.g.
    List limitedData = data.subList(0, limit);
    //...
  }
}


@RestController
public class MyController1 {

  @Autowired
  MyService myService;

  MyDto createResponse() {
    return myService.createResponse(30);
  }
}



@Controller
public class MyController2 {

  @Autowired
  MyService myService;

  MyDto createResponse() {
    return myService.createResponse(100);
  }
}

CodePudding user response:

You could look into Pageable. This interface can be used to paginate data. This way you can return a subset of 30 items, but also display how many other pages there are

  • Related