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

Spring Boot Swagger HTML文档无法显示

卜鹏
2023-03-14
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2);
    }
}
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Dec 21 03:16:26 GMT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

服务器日志

2018-12-21 03:25:14.294 DEBUG 4049 --- [  restartedMain] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
2018-12-21 03:25:14.351  INFO 4049 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-21 03:25:14.355  INFO 4049 --- [  restartedMain] c.xxxx.springboot.demo.DemoApplication   : Started DemoApplication in 3.951 seconds (JVM running for 4.723)
2018-12-21 03:25:14.373 DEBUG 4049 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Creating new Restarter for thread Thread[main,5,main]
2018-12-21 03:25:14.373 DEBUG 4049 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Immediately restarting application
2018-12-21 03:25:14.373 DEBUG 4049 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1b3b349
2018-12-21 03:25:14.373 DEBUG 4049 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Starting application com.xxxx.springboot.demo.DemoApplication with URLs [file:/Users/XXXXX/XXXXX/SpringMicroservices/SpringBoot/demo/target/classes/]
2018-12-21 03:25:14.763 DEBUG 4049 --- [on(2)-127.0.0.1] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'local.server.port' in PropertySource 'server.ports' with value of type Integer
2018-12-21 03:25:14.995  INFO 4049 --- [on(2)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-21 03:25:14.995  INFO 4049 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2018-12-21 03:25:14.995 DEBUG 4049 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2018-12-21 03:25:14.995 DEBUG 4049 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Detected AcceptHeaderLocaleResolver
2018-12-21 03:25:15.003 DEBUG 4049 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data

请在下面找到其中一个控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

@RestController
public class UserResource {

    @Autowired
    private UserDaoService service;

    @GetMapping("/users")
    public List<User> retrieveAllUsers(){
        return service.findAll();
    }

    @GetMapping("/users/{id}")
    public Resource<User> retrieveUser(@PathVariable int id){
        User user = service.findOne(id);
        if(user==null)
            throw new UserNotFoundEception("id-" + id);

        Resource<User> resource = new Resource<User>(user);
        ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
        resource.add(linkTo.withRel("all-users"));
        return resource;
    }

    @PostMapping("/users")
    public ResponseEntity<Object> CreateUser(@Valid @RequestBody User user){
        User savedUser = service.save(user);
        URI location = ServletUriComponentsBuilder
                .fromCurrentRequest().path("/{id}")
                .buildAndExpand(savedUser.getId())
                .toUri();

        return ResponseEntity.created(location).build();
    }

    @DeleteMapping("/users/{id}")
    public void deleteUser(@PathVariable int id){
        User user = service.deleteById(id);
        if(user==null)
            throw new UserNotFoundEception("id-" + id);
    }
}

共有1个答案

吴英武
2023-03-14
package com.example.config

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    public @Bean Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(paths()).build();
    }

    /**
     * Url for documentation.
     * 
     * @return
     */
    private Predicate<String> paths() {
        return regex("/basepackage of your restcontroller/*");
    }

    /**
     * Description your application
     * 
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("App title").description("App description")
                .termsOfServiceUrl("").license("").licenseUrl("").version("1.0").build();
    }
}

下面是控制器的一个示例:

@Api(value = "membre")
@RestController
public class ExampleControleur {

    @RequestMapping(value = "/find", method = RequestMethod.GET, produces = {
            MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE })
    @ApiOperation(httpMethod = "GET", value = "example text", notes = "note text ")
    @ResponseBody
    @ApiResponses(value = { @ApiResponse(code = 200, message = "OK !"),
            @ApiResponse(code = 422, message = "description."),
            @ApiResponse(code = 401, message = "description"), 
            @ApiResponse(code = 403, message = "description"),
            @ApiResponse(code = 404, message = "description"),
            @ApiResponse(code = 412, message = "description."),
            @ApiResponse(code = 500, message = "description.") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params1", value = "description.", required = true, dataType = "string", paramType = "query", defaultValue = "op"),
            @ApiImplicitParam(name = "params2", value = "description.", required = true, dataType = "string", paramType = "query")})
    public ResponseEntity<MyObject> getObjet(@RequestParam(value = "params1", required = true) Params1 params1,
            @RequestParam(value = "params2", required = true) String param2){
            }

    }

您可以使用这个类进行测试,但我想您已经在控制器中编写了所有的大摇大摆的注释!

您可以添加到主类@componentscan(BasePackages={“您的配置swagger类的包”})

 类似资料:
  • 所以我最近一直在处理PDF文档的签名,今天我遇到了一个新的奇妙的问题。因此,当我签署文档(文档实际上是在服务器上签署的)并在我的机器中打开文档时,签名显示有效,并且启用了LTV,因此几乎与预期的工作方式相同。但是,当我在老板的计算机上打开相同的文档时,它显示即使在证书被信任后也无法验证签名的身份,但如果我打开证书属性,它会说证书是有效的,吊销已经成功执行。这可能是什么原因? 图1:证书本身是可信的

  • 我无法在swagger ui中显示我的spring文档。这是我的配置: 我输入这个URL来访问接口:http://localhost:8080/swagger-ui.html 问题是它没有显示从以下位置加载的api-docs: "http://localhost:8080/v3/api-文档/招摇配置“ 状态:200。 但是如果我在url字段中输入这个,它会工作。。我的目标不是每次都进入这个。。

  • 我有一个小型Spring Boot微服务,它使用webflux将其endpoint公开为反应式。 当我运行应用程序从IntelliJ,Gradle或从cmd行与SpringDoc网络流量支持: 和我去我得到一个500错误,日志显示: 根本原因是:

  • 我有一个文档集合,其中文档的id是用户id。让我们称这些为用户文档。 每个“用户文档”包含聊天消息的子集合。但并不是所有的“用户文档”都包含任何字段(数据,而不是子集合)。 我不想返回集合中没有任何字段但有子集合的所有文档,但我觉得这是不可能的? 此代码只返回包含字段的文档,而不是只有子集合的文档?有没有办法全部返回? 如何获取集合中所有文档id的列表?空的和非空的都有?或者,如果我不能访问没有字

  • 我的要求是:在网站上,用户可以点击[我们的]平台上的签名按钮,直接弹出要签名的文档并在文档上签名 目前,根据DocuSign嵌入式发送和签名文档,我们使用JWT admin授权方法获取访问权\uu用户可以通过文档“envelopeviews:create recipient”打开并签署文档URL, 问题: 如果我们直接复制DocuSign URL并在浏览器中打开它,页面将正确显示要签名的文档页面。

  • 无法解析JSON文档:为什么我会得到这个错误。我正在尝试将参数传递给ajson主体以执行删除操作。数据使用TestNg表示法存储在一个数组中。当我运行测试时,它失败了,并显示消息“无法解析Json文档”

  • 然而,下载URL与paremeters有一些有趣的地方,它看起来如下所示: (在url中,'&\;‘实际上没有'\',但我把它放在帖子中,以避免将其转义为'&')。 那么这里是什么情况;我是否有3个参数h、e、gd,或者我有一个参数h,其值,或者我有以下3个参数值对:,,(我认为是这样,而且似乎是一个bug)? 为了形成一个正确的http请求,我需要清楚地知道参数、名称和值是什么,但是我有一个令人

  • 我的.UI中有两个树视图。一个树视图是,另一个是。现在我已经编写了一个代码,它在`driveview中显示我的系统的驱动器。我是这样做的: 现在,当我单击中的特定驱动器时,它会显示其中的子文件夹。我基本上需要做的是迭代/遍历整个驱动器,并在里面搜索文件。基本上检查所有文件夹和子文件夹。一旦它找到文件,它就应该在我的另一个树视图中显示它们,即。我为它编写了如下代码: 每当我点击有mp3文件的子文件夹