Issue
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.
Solution
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);
}
}
Answered By - Marc Stroebel
Answer Checked By - Candace Johnson (JavaFixing Volunteer)