首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未加载SpringBoot Swagger2 rest API文档

未加载SpringBoot Swagger2 rest API文档
EN

Stack Overflow用户
提问于 2017-09-25 23:43:02
回答 2查看 1K关注 0票数 2

我正在尝试获取我的小型REST API的文档,该文档是在spring中编写的,其中包含了swagger2。当我试图访问我的swagger页面时,在浏览器控制台中显示以下错误。

代码语言:javascript
复制
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 25 21:09:08 IST 2017
There was an unexpected error (type=Not Found, status=404).
No message available

我的代码片段如下所述。

代码语言:javascript
复制
    package com.example.demo;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value="/rooms")
@Api(value="rooms", tags=("rooms"))
public class RoomController {

    @Autowired
    private RoomRepositery roomRepo;

    @RequestMapping(method = RequestMethod.GET)
    @ApiOperation(value="Get All Rooms", notes="Get all rooms in the system", nickname="getRooms")
    public List<Room> findAll(@RequestParam(name="roomNumber", required=false)String roomNumber){
        if(StringUtils.isNotEmpty(roomNumber)) {
            return Collections.singletonList(roomRepo.findByRoomNumber(roomNumber));
        }
        return (List<Room>) this.roomRepo.findAll();
    }
}

App类

代码语言:javascript
复制
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

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

import static springfox.documentation.builders.PathSelectors.any;

@SpringBootApplication
@EnableSwagger2
public class RoomServiceApp {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("Room").select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                .paths(any()).build().apiInfo(new ApiInfo("Room Services",
                        "A set of services to provide data access to rooms", "1.0.0", null,
                        new Contact("Frank Moley", "https://twitter.com/fpmoles", null),null, null));
    }

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

我在这里找不到我遗漏的东西。有谁能帮我吗?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-09-26 04:30:09

对于swagger UI,您必须使用http://localhost:9090/swagger-ui.html,对于JSON API,您必须使用http://localhost:9090/v2/api-docs?group=Room

我使用了相同的代码,并找到了附加的截图。

有关更多详细信息,请参阅this项目。

票数 0
EN

Stack Overflow用户

发布于 2021-04-27 00:43:43

对于findAll方法,方法级别的请求映射应具有如下所示的URI路径。如果存在传递给方法的输入和从方法返回的输出,则还需要使用和生成。

原因是方法级别的空映射值将导致“host:port/contextPath/”,对于该值,swagger将返回404。

代码语言:javascript
复制
@RequestMapping(method = { RequestMethod.GET }, value = "/roomList", consumes = {
        MediaType.ALL}, produces = { MediaType.ALL})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46409157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档