当前位置: 首页 > 知识库问答 >
问题:

Swagger+Spring boot在http://localhost:8080/home/swagger-ui.html中没有显示APIendpoint

郜卓君
2023-03-14

我有一个spring boot应用程序在jetty+swagger上运行,当我转到http://localhost:8080/home/swagger-ui.html(见附件)时,我没有看到任何endpoint规范,即使我在那里写了一个控制器并有一个endpoint。

我的申请代码是:

package com.MyService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class MyServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GcmAuthenticationServiceApplication.class, args);
    }


}

在pom.xml中,我使用:

    <!-- Swagger 2.0 support -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.3.0</version>
            </dependency>
package com.MyService.controller;

import com.MyService.model.RegistrationData;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

@Api(value = "register", description = "the registration API")
public interface RegistrationApi {



    @ApiOperation(value = "Register user", nickname = "register", notes = "", authorizations = {
            @Authorization(value = "petstore_auth", scopes = {
                    @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"),
                    @AuthorizationScope(scope = "read:pets", description = "read your pets")
            })
    }, tags={ "register", })
    @ApiResponses(value = {
            @ApiResponse(code = 405, message = "Invalid input") })
    @RequestMapping(value = "/register",
            produces = { "application/json", "application/xml" },
            consumes = { "application/json", "application/xml" },
            method = RequestMethod.POST)
    default ResponseEntity<Void> registerUser(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )
                                              @Valid @RequestBody RegistrationData body) {

        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

}



------

package com.MyService.controller;


import com.MyService.model.RegistrationData;
import com.MyService.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class RegistrationController implements  RegistrationApi{


    private RegistrationService registrationService;


    @Autowired
    public void setProductService(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity registerUser(@RequestBody RegistrationData registrationData){
        //todo call service to rgeister
        return new ResponseEntity("user register successfully", HttpStatus.OK);
    }

}
package com.MyService.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers"))
                .paths(regex("/product.*")).build();

    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

}

共有1个答案

孟翰海
2023-03-14

您可以通过更改现有代码来使用下面的代码。基包必须指向您的控制器基包,并且URL上下文应该在您在控制器中添加的上下文中。

将基本包修改为“com.myservice.controller”,并将“/api/v1”和“/register”添加到路径regex中,

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("com.MyService.controller"))
            .paths(regex("(/api/v1.*)|(/register.*)")).build();
}
 类似资料:
  • http://localhost:8080/swagger-用户界面。html#/deosn;t显示Ui页面。错误为“406-不可接受”。但是http://localhost:8080/v2/api-文档和/或炫耀资源工作正常,没有错误。 依赖项中没有重复项。 问题应该是什么?

  • 我使用的是spring Boot2.1.6,Release and swagger 2.9.2,除了我想简化内容之外,一切都很好。 首先,我想删除标题下的: 而且,我希望和在访问时打开,直到我单击名称。

  • 此配置显示所有控制器,如何在 Swagger 上仅显示特定的控制器?

  • 我正在使用SpringFox的swagger实现。我想修改swagger-ui.html以获得一个自定义的头值。如何修改此文件?或者告诉spring fox使用备用文件?

  • 我已经把swagger融入了我的春装项目中。所有swaggerendpoint都工作正常,但< code >/product/swagger-ui . html 给出400错误。 在我的application.properties文件中,我使用了< code > server . context path =/product 。 在我的控制器中,我有以下映射,我认为这些映射导致了错误。 产品控制者

  • 我想在我的浏览器上打开招摇的用户界面。这是我的密码 但它不起作用。我仍然需要输入httpBasic()提供的基本身份验证 所以我添加了其他人找到的以下代码 现在,我可以访问localhost:8080/swagger-ui.html,但是仍然会弹出httpBasic窗口。我可以点击cancel关闭窗口,继续使用swagger ui。但是我不知道是什么导致了这个问题