一些开发问题

跨域问题

No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access.
跨域访问违反了同源策略!
同源:域名,协议,端口相同。
同源策略:浏览器的ajax只能访问跟它的html页面相同域名或IP的资源。
同源策略是浏览器的行为,是为了保护本地数据不被JavaScript代码获取回来的数据污染,因此拦截的是客户端发出的请求回来的数据接收,即请求发送了,服务器响应了,但是无法被浏览器接收。

解决方案一:

public static void main(String[] args) {
    
    
        SpringApplication.run(DemoUaaApplication.class, args);
    }
    @Bean
    public CorsFilter corsFilter() {
    
    
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*");// 允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// 允许访问的头信息,*表示全部
        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");// 允许Get的请求方法
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

解决方案二:

在类前面配置:@CrossOgigin(origins="*",maxAge=3600)

方案三:使用nginx反向代理

将url映射成同源的

方案四:使用jsonp

JSONP 是服务器与客户端跨源通信的常用方法。最大特点就是简单适用,兼容性好(兼容低版本
IE),缺点是只支持get请求,不支持post请求。
核心思想:网页通过添加一个 元素,向服务器请求 JSON 数据,服务器收到请求后,将数据放在
一个指定名字的回调函数的参数位置传回来。

axios后台获取不到前台传入的参数

axios.post(`http://localhost:8080/brands/insert`, {
    
    
                        name: this.name, 
                        date: new Date()
                    })

我们看他源码是这样的

if (utils.isObject(data)) {
    
    
	setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
	return JSON.stringify(data);
}

原因在于当参数是Object的时候axios做了两件事

  • header 被设置成 application/json;charset=utf-8
  • 参数被 Json.stringify
    所以需要和后端的小伙伴协商一下,用实体类进行接收

elasticalsearch5无法启动

报错信息

OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /tmp/hs_err_pid1.log

原因分析

这是由于elasticsearch5.0默认分配jvm空间大小为2g,内存不足以分配导致。

解决方案:

运行命令find / -name jvm.options找到jvm的选项配置文件

/var/lib/docker/overlay2/860d339b23854c34f5f1da64d672faf55f8d0d89e4d392f5e6be4c79b907b5aa/diff/usr/share/elasticsearch/config/jvm.options
/var/lib/docker/overlay2/0f5e15a32830359d506b5a58ee079e71a625fb606fc3e5586d00145ec7744d97/diff/etc/elasticsearch/jvm.options

使用命令vi 文件名 进行编辑将jvm的空间改小

## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g 改成512m
-Xmx1g 改成512m

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

pom文件变成Ignored pom.xml

问题描述:

  • 在Maven创建module时,因为初次接触,没有正确建好module所以把它删掉了!又创建了一个之前删除了的同名的module名称。但是,却弹出被忽略(Ignored)的Ignored pom.xml文件
    解决方案:
  • 点击file -> Setting -> Build,Excution,Deployment -> Build Tools -> Maven -> Ignored Files 取消勾选

npm run command

Cannot find module

  1. [webpack-cli] Error: Cannot find module ‘xxxxxx’
  • npm WARN Local package.json exists, but node_modules missing, did you mean to install?
  • 出现这样的问题你可能只是下载了模块,但是并没有安装,只需要运行npm install即可
  1. Error: Cannot find module ‘webpack-cli/bin/config-yargs’
  • 新版本的webpack-cli没有config-yargs这个文件了,需要降低webpack版本

PageHelper突然不起作用了

解决方案

在springBoot项目中你突然发现不管怎么检查,后台还是一股脑的将结果甩给你,你这时候可能需要考虑换一个pageheler了
Maven项目

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>版本号</version>
</dependency>

springBoot项目

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>版本号</version>
</dependency>

Failed to auto-configure a DataSource

Description:
	Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason:
	Failed to determine a suitable driver class
Action:
Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

解决方案

方案一:
在启动器进行配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
方案二:
修改pom文件
在dependency中添加

<exclusions>
	<exclusion>
		<!--pom坐标-->
	</exclusion>
</exclusions>

方案三:
修改配置文件

spring:
	autoconfigure:
		exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

方案四:
如果上面两种方法你都试过了,还没解决问题,那你就给他加上数据库连接的参数呗,缝缝补补又是一年

UnsatisfiedDependencyException:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘brandController’: Unsatisfied dependency expressed through field ‘brandFeign’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.zml.feign.ApiBrandFeign’: FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException

我所遇到的是因为swagger版本过低而引起的
由于之前使用的2.2.2,将其改为2.6.1问题解决

[Vue warn]: Failed to resolve directive: xxxx

(found in )
值得注意的是我们使用自定指令时,在使用的时候会将指令转化为小写,例如我定义了一个myFocus指令在使用的时候v-myFucus可能你会认为是对的,但它会报错 Failed to resolve directive: myfocus,我们可以看到指令已经变成小写的了

传输json数据给微服务请求头出错

{
    
    
    "timestamp": "2021-01-07T15:28:39.835+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/octet-stream' not supported",
    "path": "/brand/query/1/9"
}

需要更改为Content-Type=application/json

猜你喜欢

转载自blog.csdn.net/BrightZhuz/article/details/112995119