Java tutorial
/* * Copyright 2017 Danish Maritime Authority. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Obtained from https://github.com/springfox/springfox/issues/755#issuecomment-288987588 */ package net.maritimecloud.endorsement.config; 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 OperationPageableParameterReader implements OperationBuilderPlugin { private final TypeNameExtractor nameExtractor; private final TypeResolver resolver; private final ResolvedType pageableType; @Autowired public OperationPageableParameterReader(TypeNameExtractor nameExtractor, TypeResolver resolver) { this.nameExtractor = nameExtractor; this.resolver = resolver; this.pageableType = resolver.resolve(Pageable.class); } @Override public void apply(OperationContext context) { List<ResolvedMethodParameter> methodParameters = context.getParameters(); List<Parameter> parameters = newArrayList(); for (ResolvedMethodParameter methodParameter : methodParameters) { ResolvedType resolvedType = methodParameter.getParameterType(); if (pageableType.equals(resolvedType)) { ParameterContext parameterContext = new ParameterContext(methodParameter, new ParameterBuilder(), context.getDocumentationContext(), context.getGenericsNamingStrategy(), context); Function<ResolvedType, ? 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) .description("Sorting criteria in the format: property(,asc|desc). " + "Default sort order is ascending. " + "Multiple sort criteria are 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(), context.resolvedMethodParameter().getParameterType(), context.getDocumentationType(), context.getAlternateTypeProvider(), context.getGenericNamingStrategy(), context.getIgnorableParameterTypes()); return ResolvedTypes.modelRefFactory(modelContext, nameExtractor); } }