swagger 网页访问不了

来源:2-7 创建Web方法测试Swagger

孤月华

2021-01-19

老师你好 在主方法中运行没有问题 在网页输入网址后 报出
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
看了配置 都是一样的 请问下 是哪里出了问题

写回答

4回答

天海别小楼

2021-05-16

你pom.xml文件中引入的swagger依赖包是哪个版本的

0
0

孤月华

提问者

2021-01-19

这是测试类代码

package com.example.emos.wx.controller;
import com.example.emos.wx.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController

@RequestMapping("/test")
@Api("测试Web接口")
public class TestController {
   @GetMapping("/sayHello")
   @ApiOperation("最简单的测试方法")
   public R sayHello(){
       return R.ok().put("message","HelloWorld");
   }
}

0
0

孤月华

提问者

2021-01-19

这是 SwaggerConfig里面代码

package com.example.emos.wx.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
   @Bean
   public Docket createRestApi() {
       Docket docket = new Docket(DocumentationType.SWAGGER_2);
       // ApiInfoBuilder 用于在Swagger界面上添加各种信息
       ApiInfoBuilder builder = new ApiInfoBuilder();
       builder.title("EMOS在线办公系统");
       ApiInfo apiInfo = builder.build();
       docket.apiInfo(apiInfo);
       // ApiSelectorBuilder 用来设置哪些类中的方法会生成到REST API中
       ApiSelectorBuilder selectorBuilder = docket.select();
       selectorBuilder.paths(PathSelectors.any()); //所有包下的类
       //使用@ApiOperation的方法会被提取到REST API中
       selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
       docket = selectorBuilder.build();
       /*
        * 下面的语句是开启对JWT的支持,当用户用Swagger调用受JWT认证保护的方法,
        * 必须要先提交参数(例如令牌)
        */
       //存储用户必须提交的参数
       List<ApiKey> apikey = new ArrayList();
       //规定用户需要输入什么参数
       apikey.add(new ApiKey("token", "token", "header"));
       docket.securitySchemes(apikey);
       //如果用户JWT认证通过,则在Swagger中全局有效
       AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
       AuthorizationScope[] scopeArray = {scope};
       //存储令牌和作用域
       SecurityReference reference = new SecurityReference("token", scopeArray);
       List refList = new ArrayList();
       refList.add(reference);
       SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
       List cxtList = new ArrayList();
       cxtList.add(context);
       docket.securityContexts(cxtList);
       return docket;
   }
   



   }

0
1
慕先生6295114
application.yml context-path: 你的项目名字是不是改了,相应改一下
2021-06-14
共1条回复

神思者

2021-01-19

检查Swagger配置部分的代码,以及Swagger相关注解是否写对了

0
4
weixin_慕村1392198
回复
神思者
老师,我输入http://127.0.0.1:8080/emos-wx-api/swagger-ui.html,提示404,是怎么回事呀
2021-08-24
共4条回复

SpringBoot 在线协同办公小程序开发 全栈式项目实战

未来趋势型业务 + 前后端综合技术栈 + 惊艳的细节打磨

1798 学习 · 1915 问题

查看课程