Selaa lähdekoodia

提交解决swagger无效的问题

dev
yeqd 4 vuotta sitten
vanhempi
commit
53834ff5e1
3 muutettua tiedostoa jossa 16 lisäystä ja 9 poistoa
  1. +2
    -0
      user-service/pom.xml
  2. +8
    -4
      user-service/src/main/java/com/hp/user/service/UserServiceApplaction.java
  3. +6
    -5
      user-service/src/main/java/com/hp/user/service/config/SwaggerConfig.java

+ 2
- 0
user-service/pom.xml Näytä tiedosto

@@ -49,6 +49,8 @@
<exclusion>
<artifactId>spring-boot-starter-json</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>


+ 8
- 4
user-service/src/main/java/com/hp/user/service/UserServiceApplaction.java Näytä tiedosto

@@ -1,12 +1,12 @@
package com.hp.user.service;

import cn.nyhlw.doc2swagger.springswagger2.EnableSwagger2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.ComponentScan;

/**
* 用户服务启动类
@@ -14,11 +14,15 @@ import org.springframework.context.annotation.ComponentScan;
* @since 2020/11/18
*
*/
@EnableSwagger2
@MapperScan("com.hp.user.service.dao") //扫描的mapper
@SpringBootApplication
@NacosPropertySource(dataId = "userdatasource", autoRefreshed = true)
@ComponentScan("com.hp.user.service.config")
//不能加compentscan,加了
@ComponentScan("com.hp.user.service.dao")//这个地方不能写错,写错的话会出现2个问题,如果写的路径扫描不到swaggerconfig那么页面会报错
//(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: ),
//如果写的路径扫描不到controller那么页面不会有接口信息
//所以最好不要写
public class UserServiceApplaction {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplaction.class, args);


+ 6
- 5
user-service/src/main/java/com/hp/user/service/config/SwaggerConfig.java Näytä tiedosto

@@ -1,21 +1,22 @@
package com.hp.user.service.config;

import java.util.Arrays;

import cn.nyhlw.doc2swagger.springswagger2.EnableSwagger2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.nyhlw.doc2swagger.core.config.RestDocConfig;
//import cn.nyhlw.doc2swagger.springswagger2.EnableSwagger2;
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;

//这两个注释一定要有,没有以及EnableSwagger2依赖错误会报
//(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: ),
@Configuration
@EnableSwagger2
@EnableSwagger2//并且这个注释要特别注意不能使用cn.nyhlw.doc2swagger.springswagger2.EnableSwagger2;一定要使用springfox.documentation.swagger2.annotations.EnableSwagger2;
public class SwaggerConfig {
public static final String VERSION = "1.0.0";
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.hp.user.service.controller";


Ladataan…
Peruuta
Tallenna