gpt4 book ai didi

java - SpringBoot Swagger2 REST API 文档未加载

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:45 25 4
gpt4 key购买 nike

我只是想获取在 spring 中编写的带有 swagger2 包含的小型 REST API 的文档。当我尝试访问我的 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

下面提到了我的代码片段。

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

应用程序类

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

我无法找到我在这里缺少的东西。谁能帮帮我吗?

谢谢

最佳答案

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

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

enter image description here

引用this项目了解更多详细信息。

关于java - SpringBoot Swagger2 REST API 文档未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com