在虚张声势中,页面无法正确显示

时间:2018-06-04 08:58:38

标签: spring spring-boot swagger-ui swagger-2.0

当我使用我的弹簧启动将swagger更新为swagger2时,它显示barcodeImage = b.Encode(BarcodeLib.TYPE.CODE128, "001234", Color.Black, Color.White, 113, 18); Bitmap SkuImage = new Bitmap(113, 18, System.Drawing.Imaging.PixelFormat.Format32bppArgb); RectangleF rectf = new RectangleF(10, 5, 113, 18); Graphics graphics = Graphics.FromImage(SkuImage); // SkuImage.SetPixel(10,10,Color.Blue); graphics.DrawString(StringToEncode, new Font("Arial", 4), Brushes.Black, rectf); b.SaveImage(MemStream, savetype); MemStream.Close(); barcodeImage.Dispose(); SkuImage.Save(imgSkupath); SkuImage.Dispose(); g.Clear(Color.White); //here change BG color of Image g.DrawImage(Image.FromFile("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/" + Filename), new Point(15, 15)); g.DrawImage(Image.FromFile("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/Sku.jpg"), new Point(25, 30)); img.Save("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/FinalImage.jpeg", ImageFormat.Jpeg); File.Delete("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/Sku.jpg"); File.Delete("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/" + Filename); 类型的正确参数时应显示pageablepage而不是它开始显示size }和pageSize在其他方面不正确。

enter image description here

我没有手动更改任何内容但由于某种原因,它显示错误的参数名称。

pageNumber

pom是

 return new Docket(DocumentationType.SWAGGER_2)
            .groupName("Rest API")
            .securitySchemes(Collections.singletonList(new BasicAuth(BASIC_AUTH)))
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .paths(s -> oneOf(
                "/some/**",
                "/search-controller/**").test(s))
            .build();

控制器如下所示

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-data-rest</artifactId>
      <version>2.9.0</version>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.0</version>
    </dependency>

4 个答案:

答案 0 :(得分:4)

https://github.com/springfox/springfox/issues/755#issuecomment-393378205

下面是创建规则的示例,该规则自动提供用于配置Pageable类型的约定。

@Configuration
public class SwaggerConfig {

    @Bean
    public AlternateTypeRuleConvention pageableConvention(
            final TypeResolver resolver) {
        return new AlternateTypeRuleConvention() {

            @Override
            public int getOrder() {
                return Ordered.HIGHEST_PRECEDENCE;
            }

            @Override
            public List<AlternateTypeRule> rules() {
                return Arrays.asList(
                        newRule(resolver.resolve(Pageable.class), resolver.resolve(pageableMixin()))
                );
            }
        };
    }

    private Type pageableMixin() {
        return new AlternateTypeBuilder()
                .fullyQualifiedClassName(
                        String.format("%s.generated.%s",
                                Pageable.class.getPackage().getName(),
                                Pageable.class.getSimpleName()))
                .withProperties(Arrays.asList(
                        property(Integer.class, "page"),
                        property(Integer.class, "size"),
                        property(String.class, "sort")
                ))
                .build();
    }

    private AlternateTypePropertyBuilder property(Class<?> type, String name) {
        return new AlternateTypePropertyBuilder()
                .withName(name)
                .withType(type)
                .withCanRead(true)
                .withCanWrite(true);
    }
}

答案 1 :(得分:0)

我在升级到springfox 2.9.0后看到了相同的行为而没有更改任何代码。看来springfox在Spring控制器方法中遇到Pageable接口作为请求参数时,正在向Swagger文档中添加pageSize,pageNumber和offset。

答案 2 :(得分:0)

下面是我的Swagger配置类,它工作正常。

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.archisoft.mtx.controller"))
                .paths(regex("/api.*"))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "Backend API Services",
                "Backend APIs for Business to Business",
                "V1.0",
                "Terms of service",
                "Sadun | Tharanga email",
                "Archisoft Global",
                "Archisoft URL");
        return apiInfo;
    }

    @Bean
    public AlternateTypeRuleConvention pageableConvention(
            final TypeResolver resolver,
            final RepositoryRestConfiguration restConfiguration) {
        return new AlternateTypeRuleConvention() {

            @Override
            public int getOrder() {
                return Ordered.HIGHEST_PRECEDENCE;
            }

            @Override
            public List<AlternateTypeRule> rules() {
                return singletonList(
                        newRule(resolver.resolve(Pageable.class), resolver.resolve(pageableMixin()))
                );
            }
        };
    }

    private Type pageableMixin() {
        return new AlternateTypeBuilder()
                .fullyQualifiedClassName(
                        String.format("%s.generated.%s",
                                Pageable.class.getPackage().getName(),
                                Pageable.class.getSimpleName()))
                .withProperties(Stream.of(
                        property(Integer.class, "page"),
                        property(Integer.class, "size"),
                        property(String.class, "sort")
                ).collect(toList()))
                .build();
    }

    private AlternateTypePropertyBuilder property(Class<?> type, String name) {
        return new AlternateTypePropertyBuilder()
                .withName(name)
                .withType(type)
                .withCanRead(true)
                .withCanWrite(true);
    }
}

答案 3 :(得分:0)

这是我对前两个示例的扩展。它还包含ApiParam批注值,并且可以通过SpringDataWebProperties设置进行配置。

在扩展字段之前: Before Field Expansion

扩展字段后: After Field Expansion

有几种使用Swagger AlternateTypeRules的方法。可以将它们直接添加到Swagger文件夹中,可以通过几种方式将其作为Bean添加。受以上示例的启发,这是我的操作方式,它使用注释代理(如下所示)包括其他ApiParam数据:

@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    @Bean
    public AlternateTypeRuleConvention springDataWebPropertiesConvention(final SpringDataWebProperties webProperties) {
        return new AlternateTypeRuleConvention() {
            @Override
            public int getOrder() { return Ordered.HIGHEST_PRECEDENCE; }

            @Override
            public List<AlternateTypeRule> rules() {
                return singletonList(
                        newRule(Pageable.class, pageableDocumentedType(webProperties.getPageable(), webProperties.getSort()))
                );
            }
        };
    }

    private Type pageableDocumentedType(SpringDataWebProperties.Pageable pageable, SpringDataWebProperties.Sort sort) {
        final String firstPage = pageable.isOneIndexedParameters() ? "1" : "0";
        return new AlternateTypeBuilder()
                .fullyQualifiedClassName(fullyQualifiedName(Pageable.class))
                .property(property(pageable.getPageParameter(), Integer.class, ImmutableMap.of(
                        "value", "Page " + (pageable.isOneIndexedParameters() ? "Number" : "Index"),
                        "defaultValue", firstPage,
                        "allowableValues", String.format("range[%s, %s]", firstPage, Integer.MAX_VALUE),
                        "example", firstPage
                )))
                .property(property(pageable.getSizeParameter(), Integer.class, ImmutableMap.of(
                        "value", "Page Size",
                        "defaultValue", String.valueOf(pageable.getDefaultPageSize()),
                        "allowableValues", String.format("range[1, %s]", pageable.getMaxPageSize()),
                        "example", "5"
                )))
                .property(property(sort.getSortParameter(), String[].class, ImmutableMap.of(
                        "value", "Page Multi-Sort: fieldName,(asc|desc)"
                )))
                .build();
    }

    private String fullyQualifiedName(Class<?> convertedClass) {
        return String.format("%s.generated.%s", convertedClass.getPackage().getName(), convertedClass.getSimpleName());
    }

    private AlternateTypePropertyBuilder property(String name, Class<?> type, Map<String, Object> parameters) {
        return new AlternateTypePropertyBuilder()
                .withName(name)
                .withType(type)
                .withCanRead(true)
                .withCanWrite(true)
                .withAnnotations(Collections.singletonList(AnnotationProxy.of(ApiParam.class, parameters)));
    }
}

为了添加其他数据,例如defaultValue和allowableValues,必须使用.withAnnotations()方法,该方法需要一个注释代理。有几种可用的,这是我的(使用龙目岛):

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Accessors(fluent = true)
public class AnnotationProxy implements Annotation, InvocationHandler {
    @Getter
    private final Class<? extends Annotation> annotationType;
    private final Map<String, Object> values;

    public static <A extends Annotation> A of(Class<A> annotation, Map<String, Object> values) {
        return (A) Proxy.newProxyInstance(annotation.getClassLoader(),
                new Class[]{annotation},
                new AnnotationProxy(annotation, new HashMap<String, Object>(values) {{
                    put("annotationType", annotation); // Required because getDefaultValue() returns null for this call
                }}));
    }

    public Object invoke(Object proxy, Method method, Object[] args) {
        return values.getOrDefault(method.getName(), method.getDefaultValue());
    }
}