Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The problem of Pageable parameters in Spring Boot Integrated SpringFox Swagger

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Springfox-swagger is often used in Spring Boot projects to generate REST API documents and springfox-swagger-ui is used for API testing.

Io.springfox springfox-swagger2 2.8.0 io.springfox springfox-swagger-ui 2.8.0

When the parameters of the REST API method contain org.springframework.data.domain.Pageable, if there is no other configuration, Swagger generates parameters such as pageNumber, pageSize, offset, paged, unpaged, sort.sorted, sort.unsorted and so on according to the get/is method of the interface Pageable, but in fact these parameters are invalid.

ApiOperation (value = "Find airlines") @ GetMapping (value = "/ airlines") public Page searchAirlines (Airline airline, Pageable pageable) {return repository.findAll (org.springframework.data.domain.Example.of (airline), pageable);}

For the process of parsing Pageable parameters by Spring Boot, please see the resolveArgument () method of org.springframework.data.web.PageableHandlerMethodArgumentResolver:

@ Overridepublic Pageable resolveArgument (MethodParameter methodParameter, @ Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @ Nullable WebDataBinderFactory binderFactory) {assertPageableUniqueness (methodParameter); Optional defaultOrFallback = getDefaultFromAnnotationOrFallback (methodParameter). ToOptional (); String pageString = webRequest.getParameter (getParameterNameToUse (pageParameterName, methodParameter)); String pageSizeString = webRequest.getParameter (getParameterNameToUse (sizeParameterName, methodParameter)); Optional page = parseAndApplyBoundaries (pageString, Integer.MAX_VALUE, true); Optional pageSize = parseAndApplyBoundaries (pageSizeString, maxPageSize, false) If (! (page.isPresent () & & pageSize.isPresent ()) & &! defaultOrFallback.isPresent ()) {return Pageable.unpaged ();} int p = page.orElseGet (()-> defaultOrFallback.map (Pageable::getPageNumber) .orElseThrow (IllegalStateException::new)); int ps = pageSize.orElseGet ()-> defaultOrFallback.map (Pageable::getPageSize) .orElseThrow (IllegalStateException::new)); / / Limit lower bound ps = ps

< 1 ? defaultOrFallback.map(Pageable::getPageSize).orElseThrow(IllegalStateException::new) : ps; // Limit upper bound ps = ps >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report