swagger ui未在ui中显示post方法

rseugnpd  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(312)

我正在使用带有简单rest控制器的springfox 3.0。在swagger ui中,只显示get方法,而不显示post方法。

我也在努力在swagger ui中获得上下文路径。实际上,上下文路径是在端点之后而不是之前追加的。所以我把所有东西都放在tomcat根文件夹中,没有上下文。

<servlet-mapping>
        <servlet-name>SwaggerRest</servlet-name>
        <url-pattern>/api/*</url-pattern>
</servlet-mapping>
@Bean
    public Docket api() { 
         return new Docket(DocumentationType.SWAGGER_2)
                  .host("localhost:8080")
                  .select()                                  
                  .apis(RequestHandlerSelectors.basePackage("test.maventest")).paths(PathSelectors.ant("/**"))              

                  .build();      

-------------
@Component
// make sure it runs after the default plugin springfox.documentation.swagger2.web.WebMvcBasePathAndHostnameTransformationFilter
@Order(Ordered.HIGHEST_PRECEDENCE + 1000)
public class CustomMvcPathHostFilter implements WebMvcSwaggerTransformationFilter {

    @Override
    public Swagger transform(SwaggerTransformationContext<HttpServletRequest> context) {
        Swagger swagger = context.getSpecification();
        context.request().ifPresent(servletRequest -> {
            UriComponents uriComponents = HostNameProvider.componentsFrom(servletRequest, swagger.getBasePath());

            swagger.host(uriComponents.getHost() + (uriComponents.getPort() > 0 ? ":" + uriComponents.getPort() : ""));

            // set Swagger base path
            String basePath = StringUtils.isEmpty(uriComponents.getPath()) ? "/" : uriComponents.getPath();
            swagger.basePath(basePath); 
            System.out.println("baseoath "+basePath);
            // rewrite paths to remove the leading base path from each endpoint
            final Map<String, Path> newPaths = swagger.getPaths().entrySet().stream().collect(Collectors.toMap(
                    entry -> entry.getKey().replaceAll("^" + basePath, ""), Map.Entry::getValue));
            swagger.setPaths(newPaths);
        });
        return swagger;
    }

//Not showing in swagger ui

@PostMapping("/saveInfo")
    public ResponseEntity <?>  saveinFo(@RequestBody Student student) throws Exception {

        System.out.println("Saving  studinfo Response at ==>444444444" );
        return new  ResponseEntity <List<Student>> (studservice.save(student),HttpStatus.OK);
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题