Springboot整合swagger2项目的部署问题
2017-09-04 10:11
1971 查看
转自:http://blog.sina.com.cn/s/blog_13b4eadda0102xaam.html
最近写了一个小项目,后端是springboot来搭建环境架构,前端是用ionic。要求是前后端分离开发,为了不想写接口文档就整合了swagger2。在这个过程中遇到了很多坑。
其中一个就是在本地服务器好好运行的项目,但是部署到自己的云服务器就抛出了拒绝访问错误。
MLHttpRequest cannot load http://127.0.01/api/user/logout. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.
这说明不能加载这个链接,跨域了。然后会去想如何解决跨域问题。其实关键点在swagger-ui自动生成接口文档链接的原理。
当项目部署到云服务器时,接口的链接应该是http://服务器地址:端口/资源地址,但是接口生成链接为127.0.0.1,说明swagger默认配置了为本地地址,即localhost。
swagger原理是根据@RestController("")中的资源地址,拼接上一个前缀,生成最终的接口来链接。因此,在部署到云服务器时,配置前缀为服务器的地址就好了。
方法一:在application.yml中配置sop.swagger.host
这样子就可以了。
前面的设置只是可以用swagger-ui来测试自己的接口,把接口部署到云服务器后,前端开发人员,将在前端页面调用接口来测试,还是存在跨域的问题。这是就要设置一个响应头response.setHeader("Access-Control-Allow-Origin", "*");来实现java的跨域访问。
定义一个拦截器:
@Component
public class CORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 添加跨域CORS
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
return true;
}
}
拦截器配置到web.xml中
下面使用的是springboot的配置
@Configuration
public class CrossDomain extends WebMvcConfigurerAdapter {
@Autowired
private CORSInterceptor corsInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(corsInterceptor);
}
}
最近写了一个小项目,后端是springboot来搭建环境架构,前端是用ionic。要求是前后端分离开发,为了不想写接口文档就整合了swagger2。在这个过程中遇到了很多坑。
其中一个就是在本地服务器好好运行的项目,但是部署到自己的云服务器就抛出了拒绝访问错误。
MLHttpRequest cannot load http://127.0.01/api/user/logout. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.
这说明不能加载这个链接,跨域了。然后会去想如何解决跨域问题。其实关键点在swagger-ui自动生成接口文档链接的原理。
当项目部署到云服务器时,接口的链接应该是http://服务器地址:端口/资源地址,但是接口生成链接为127.0.0.1,说明swagger默认配置了为本地地址,即localhost。
swagger原理是根据@RestController("")中的资源地址,拼接上一个前缀,生成最终的接口来链接。因此,在部署到云服务器时,配置前缀为服务器的地址就好了。
方法一:在application.yml中配置sop.swagger.host
这样子就可以了。
前面的设置只是可以用swagger-ui来测试自己的接口,把接口部署到云服务器后,前端开发人员,将在前端页面调用接口来测试,还是存在跨域的问题。这是就要设置一个响应头response.setHeader("Access-Control-Allow-Origin", "*");来实现java的跨域访问。
定义一个拦截器:
@Component
public class CORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 添加跨域CORS
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
return true;
}
}
拦截器配置到web.xml中
下面使用的是springboot的配置
@Configuration
public class CrossDomain extends WebMvcConfigurerAdapter {
@Autowired
private CORSInterceptor corsInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(corsInterceptor);
}
}
相关文章推荐
- springboot项目打成war包部署到tomcat遇到的一些问题
- intellij idea2016 linux springboot项目下解决热部署问题
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
- Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结
- IntelliJ IDEA中Spring Boot项目使用spring-boot-devtools无法实现热部署/热更新的问题解决
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- 关于springboot项目的部署问题
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- springboot中整合activiti流程设计器时遇到访问项目需要输入tomcat用户名和密码的问题
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- 关于springBoot项目在IDEA中热部署问题
- springboot项目配置多环境打包部署遇到的问题总结
- Spring-Boot1.4.0项目部署问题小记
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- Tomcat 部署 Spring Boot 项目 报错 404
- 解决SpringBoot整合shiro之后无法加载静态资源文件问题
- Springboot整合Mybatis的CallSettersOnNulls配置问题
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目