pageable parameter issues for Spring boot integrated swagger

Source: Internet
Author: User

Using pageable as an API method parameter, such as no other action, does not include the pageable parameter in the Swagger API docs and swagger UI. (Springfox-swagger2 2.8.0)

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

There are several ways to resolve this:

@ApiImplicitParams

Each method has to be added, more cumbersome.

@ApiOperation(value = "Find airlines", response = Collection.class)@ApiImplicitParams({    @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query", value = "Results page you want to retrieve (0..N)"),    @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query", value = "Number of records per page."),    @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",        value = "Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.")})@GetMapping(value = "/airlines")public List<Airline> searchAirlines(Airline airline, Pageable pageable) {    Page<Airline> airlines = repository.findAll(org.springframework.data.domain.Example.of(airline), pageable);    return airlines.getContent();}
Operationbuilderplugin
Import Com.fasterxml.classmate.resolvedtype;import Com.fasterxml.classmate.typeresolver;import Com.google.common.base.function;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.core.ordered;import Org.springframework.core.annotation.order;import Org.springframework.data.domain.pageable;import Org.springframework.stereotype.component;import Springfox.documentation.builders.parameterbuilder;import Springfox.documentation.schema.resolvedtypes;import Springfox.documentation.schema.typenameextractor;import Springfox.documentation.schema.modelreference;import Springfox.documentation.service.parameter;import Springfox.documentation.service.resolvedmethodparameter;import Springfox.documentation.spi.DocumentationType; Import Springfox.documentation.spi.schema.contexts.modelcontext;import Springfox.documentation.spi.service.operationbuilderplugin;import Springfox.documentation.spi.service.contexts.operationcontext;import Springfox.documEntation.spi.service.contexts.parametercontext;import Static Com.google.common.collect.lists.newarraylist;import static Springfox.documentation.spi.schema.contexts.ModelContext.inputParam; @Component @order (ordered.lowest_ Precedence) public class Pageableparameterreader implements Operationbuilderplugin {private final typenameextractor nam    Eextractor;    Private final typeresolver resolver;    Private final Resolvedtype Pageabletype; @Autowired public Pageableparameterreader (typenameextractor nameextractor, typeresolver resolver) {This.nameext        Ractor = Nameextractor;        This.resolver = resolver;    This.pageabletype = Resolver.resolve (Pageable.class); } @Override public void apply (OperationContext context) {list<resolvedmethodparameter> Methodparameter        s = context.getparameters ();        list<parameter> parameters = Newarraylist (); for (Resolvedmethodparameter methodparameter:methodparameters) {Resolvedtype Resolvedtype = Methodparameter.getparametertype (); if (Pageabletype.equals (Resolvedtype)) {Parametercontext Parametercontext = new Parametercontext (methodPar                        Ameter, New ParameterBuilder (), Context.getdocumentationcontext (),                Context.getgenericsnamingstrategy (), context); Function<resolvedtype, huh?                Extends modelreference> factory = Createmodelreffactory (Parametercontext);                Modelreference Intmodel = factory.apply (Resolver.resolve (Integer.type));                Modelreference Stringmodel = factory.apply (Resolver.resolve (List.class, String.class));                        Parameters.Add (New ParameterBuilder (). ParameterType ("Query"). Name ("page") . Modelref (Intmodel). Description ("Results page you want to retrieve (0..N)")                . build ()); Parameters.Add (New ParAmeterbuilder (). ParameterType ("Query"). Name ("size")                . Modelref (Intmodel). Description ("Number of records per page"). Build ());                        Parameters.Add (New ParameterBuilder (). ParameterType ("Query"). Name ("Sort") . Modelref (Stringmodel). AllowMultiple (True). descript                                Ion ("Sorting criteria in the Format:property (, Asc|desc)."                                + "Default sort order is ascending."                        + "Multiple sort criteria is supported.")                . build ());            Context.operationbuilder (). parameters (parameters);    }}} @Override public Boolean supports (Documentationtype delimiter) {return true; } Private Function<resolvedtype,? Extends Modelreference> createmodelreffactory (parameteRcontext context) {Modelcontext Modelcontext = Inputparam (Context.getgroupname (), C Ontext.resolvedmethodparameter (). Getparametertype (), Context.getdocumentationtype (), context . Getalternatetypeprovider (), Context.getgenericnamingstrategy (), Context.getignorableparamet        Ertypes ());    Return Resolvedtypes.modelreffactory (Modelcontext, nameextractor); }}
Alternatetyperuleconvention
Import Com.fasterxml.classmate.typeresolver;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.core.ordered;import Org.springframework.data.domain.pageable;import Springfox.documentation.builders.alternatetypebuilder;import Springfox.documentation.builders.alternatetypepropertybuilder;import Springfox.documentation.schema.alternatetyperule;import Springfox.documentation.schema.alternatetyperuleconvention;import Java.lang.reflect.type;import java.util.List; Import static Com.google.common.collect.lists.newarraylist;import static Springfox.documentation.schema.AlternateTypeRules.newRule, @Configuration @suppresswarnings (" Springjavaautowiringinspection ") public class Springdataconfiguration {@Bean public alternatetyperuleconvention page            Ableconvention (Final typeresolver resolver) {return new alternatetyperuleconvention () {@Override                public int GetOrder () {return ordered.lowest_precedence; } @Override Public list<alternatetyperule> rules () {return newarraylist (Newrul            E (Resolver.resolve (Pageable.class), Resolver.resolve (Pageablemixin ()));    }        }; } private Type Pageablemixin () {return new Alternatetypebuilder (). Fullyqualifiedclassname (Strin G.format ("%s.generated.%s", Pageable.class.getPackage (). GetName (), Pageable.class.getSimpleName ()). WITHPR Operties (Newarraylist (Integer.class, "page"), Property (Integer.class, "size"), Property (String.class, "    Sort ")). Build (); } Private Alternatetypepropertybuilder Property (class<?> type, String name) {return new Alternatetypeprop                Ertybuilder (). Withname (name). Withtype (Type). Withcanread (True)    . Withcanwrite (True); }}

This is the method described in the Springfox documentation, which creates a in-memory type, how do I add a description? Please advise me if you have used it.
or customize a class bar:

Import Com.fasterxml.classmate.typeresolver;import Io.swagger.annotations.apimodel;import Io.swagger.annotations.apimodelproperty;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.core.ordered;import Org.springframework.data.domain.pageable;import Springfox.documentation.schema.alternatetyperule;import Springfox.documentation.schema.alternatetyperuleconvention;import Java.util.list;import Static Com.google.common.collect.lists.newarraylist;import Static Springfox.documentation.schema.AlternateTypeRules.newRule, @Configuration @suppresswarnings (" Springjavaautowiringinspection ") public class Springdataconfiguration {@Bean public alternatetyperuleconvention page            Ableconvention (Final typeresolver resolver) {return new alternatetyperuleconvention () {@Override            public int GetOrder () {return ordered.highest_precedence;      } @Override      Public list<alternatetyperule> Rules () {return newarraylist (Newrule (Resolver.resolve (pageable.            Class), Resolver.resolve (Page.class));    }        }; } @ApiModel static Class page {@ApiModelProperty ("Results page want to retrieve (0..N)") Private        Integer page;        @ApiModelProperty ("Number of records per page") Private Integer size; @ApiModelProperty ("Sorting criteria in the Format:property (, Asc|desc). Default sort order is ascending.        Multiple sort criteria is supported. ")        Private list<string> sort;        Public Integer GetPage () {return page;        } public void Setpage (Integer page) {this.page = page;        } public Integer GetSize () {return size;        } public void SetSize (Integer size) {this.size = size;        } public list<string> Getsort () {return sort; } public void Setsort(list<string> sort)        {this.sort = sort; }    }}

Spring Boot integrated swagger pageable parameter issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.