前后端传参中遇见的问题

前后端传参经常容易出错,本文记录开发springBoot+Mybatis-plus+vuecli项目中出现的传参问题及解决办法

1.前后端没有跨域配置,报错

解决方法:后端进行跨域配置,拷贝CorsConfig类

java 复制代码
package com.example.xxxx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 全局配置可跨域访问api接口
 */

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); //允许任何域名
        corsConfiguration.addAllowedHeader("*"); //允许任何头
        corsConfiguration.addAllowedMethod("*"); //允许任何方法
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); //注册
        return new CorsFilter(source);
    }
}

2.longtext类型 前端json内容包含转义字符,报错

WARN 9296 --- nio-8090-exec-4 .w.s.m.s.DefaultHandlerExceptionResolver : Resolved org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\ at \[Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 2, column: 15 (through reference chain: com.example.memo.entity.Note"content")]

出现原因:Json 字符中有有些字段比如空格、反斜杠、换行符等一些特殊字符,但是 Json 框架没有对这些字符进行处理,就会导致出现错误。

解决方法:加入 fastjson 依赖

html 复制代码
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>

在配置文件中加入

java 复制代码
spring:
	jackson:
	    parser:
	      allow-unquoted-control-chars: true

重启项目,运行成功!

相关推荐
我命由我123455 小时前
CesiumJS 笔记 - 获取容器中心点、Cartesian3 clone 方法、修改 Cartesian3 对象的高度
前端·javascript·css·前端框架·html·html5·js
AI_AGENT_DEV_AI5 小时前
原生 APP 开发的开发优点
uni-app
Hopebearer_6 小时前
页面突然只剩 DOM?一次静态资源版本错配排查
前端·部署
东风破_6 小时前
ESLint 是什么?为什么你的项目需要它?
前端·后端·代码规范
嘻哈∠※7 小时前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端
白狐_7987 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
圣殿骑士-Khtangc8 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
zander2588 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
码匠许师傅8 小时前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
BigTopOne9 小时前
【ijkplayer】 硬解码流程
前端