Srping Boot 引入swagger 报错Unable to infer base url. This is common when using

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:

 

springboot 1.5.2 引入swagger 2.8.0报错

@EnableSwagger2这个在启动程序那里已经加上

去作者页面去查看,说是在2.8.1 版本已经修复,但在maven中没成功加载,很多人都说是版本问题,而且说加@EnableSwagger2,都已经试过,全失败了

 

利用springboot  2.0.3.RELEASE 版本,引入

pom.xml中



    io.springfox
    springfox-swagger2
    2.8.0


    io.springfox
    springfox-swagger-ui
    2.8.0

 

SwaggerConfig文件中

package com.wsmyskxz.springboot;

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

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wsmyskxz.springboot.Controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("营养探索馆-客服系统")
                .description("powered by By-Health")
                .termsOfServiceUrl("http://www.xxx.com/")
                //.contact(contact)
                .version("1.0")
                .build();
    }

}

 

CheckCenterController文件代码

 

package com.wsmyskxz.springboot.Controller;

import com.wsmyskxz.springboot.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Api("客服接口")
@Controller
@RequestMapping("/checkcenter")
public class CheckCenterController {


    @ApiOperation(value="获取指定id用户详细信息", notes="根据user的id来获取用户详细信息")
    @ApiImplicitParam(name = "id",value = "用户id", dataType = "String", paramType = "path")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Long id) {

        User user = new User();
        user.setId(id);
        user.setPassword("abc");
        user.setUserName("12345");
        return user;
    }


}

 

User  上面那个controller会调用到,这个文件无关紧要,最重要的是版本,springboot版本为2.0以上应该都没有什么问题

package com.wsmyskxz.springboot.model;

import java.sql.Date;

public class User {

   private long id;
   private String userName;
   private String password;
   private String realName;
   private int age;
   private String phoneNumber;
   private String headPicture;
   private Date addDate;
   private Date updateDate;
   private int state;

   @Override
   public String toString() {
      return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", realName=" + realName
            + ", age=" + age + ", phoneNumber=" + phoneNumber + ", headPicture=" + headPicture + ", addDate="
            + addDate + ", updateDate=" + updateDate + ", state=" + state + "]";
   }

   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   public String getUserName() {
      return userName;
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getRealName() {
      return realName;
   }

   public void setRealName(String realName) {
      this.realName = realName;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public String getPhoneNumber() {
      return phoneNumber;
   }

   public void setPhoneNumber(String phoneNumber) {
      this.phoneNumber = phoneNumber;
   }

   public String getHeadPicture() {
      return headPicture;
   }

   public void setHeadPicture(String headPicture) {
      this.headPicture = headPicture;
   }

   public Date getAddDate() {
      return addDate;
   }

   public void setAddDate(Date addDate) {
      this.addDate = addDate;
   }

   public Date getUpdateDate() {
      return updateDate;
   }

   public void setUpdateDate(Date updateDate) {
      this.updateDate = updateDate;
   }

   public int getState() {
      return state;
   }

   public void setState(int state) {
      this.state = state;
   }

}

 

项目文件目录

Srping Boot 引入swagger 报错Unable to infer base url. This is common when using_第1张图片

 

 

启动项目后访问 http://127.0.0.1:8082/swagger-ui.html

Srping Boot 引入swagger 报错Unable to infer base url. This is common when using_第2张图片

 

 

 

你可能感兴趣的:(spring,boot,java)