栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot项目中 Swagger配置 以及 跨域配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot项目中 Swagger配置 以及 跨域配置

1、配置pom.xml


     io.springfox
     springfox-swagger2
     2.9.2


     io.springfox
     springfox-swagger-ui
     2.9.2

 2、粘贴Swagger2代码

首先建立config文件夹,然后新建Swagger2.java文件,粘贴代码

package com.example.szlzzhcu_ht.config;

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 Swagger2 {
	@Bean
    public Docket createRestApi() {
        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo())  //apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
                .select()            //返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
                为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.nercita.szlzzhc"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .version("1.0")
                .build();
    }
}

http://localhost:端口号/swagger-ui.html#! 启动项目数据网址就能点开,电脑端口看Tomcat启动后的显示,目前为8080;

网站输入连接进行测试,如果出现以下问题

出现错误:When allowCredentials is true, allowedOrigins cannot contain the special value “*“ since that cannot

 在较新的springboot版本中,跨域配置需要将 .allowedOrigins 替换成 .allowedOriginPatterns

修改config——>CORSConfig.java

修改后:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号