简体   繁体   English

SpringBoot Swagger2 rest API 文档未加载

[英]SpringBoot Swagger2 rest API documentation is not loading

I am just trying out to get the documentation for my small REST API written in in spring with swagger2 inclusion.我只是想获取我在春季编写的包含 swagger2 的小型 REST API 的文档。 When i try to access my swagger page the following error is shown in browser console.当我尝试访问我的 swagger 页面时,浏览器控制台中会显示以下错误。

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

My code snippets are below mentioned.下面提到了我的代码片段。

    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 class应用类

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);
    }
}

I am not able to found what i am missing here.我无法在这里找到我缺少的东西。 Can any one help me out?任何人都可以帮助我吗?

Thanks谢谢

You have to use http://localhost:9090/swagger-ui.html for swagger UI and http://localhost:9090/v2/api-docs?group=Room for JSON API.您必须将http://localhost:9090/swagger-ui.html用于 swagger UI 和http://localhost:9090/v2/api-docs?group=Room用于 JSON API。

I used the same code and find attached screenshot.我使用了相同的代码并找到了附加的屏幕截图。

在此处输入图片说明

refer to this project for more details.有关更多详细信息,请参阅项目。

Request mapping at method level should have URI path like below for findAll method.对于 findAll 方法,方法级别的请求映射应具有如下所示的URI 路径 Consumes and produces also required if there is input passed to method and output returned from method.如果有传递给方法的输入和从方法返回的输出,则也需要使用和生产。

The reason is empty mapping value at method level will result in 'host:port/contextPath/' for which swagger will return 404.原因是方法级别的空映射值将导致 'host:port/contextPath/' swagger 将返回 404。

@RequestMapping(method = { RequestMethod.GET }, value = "/roomList", consumes = {
        MediaType.ALL}, produces = { MediaType.ALL})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM