在使用Spring或Spring Boot时一些特殊的参数会被转义,或者因转义导致出现异常情况,本文汇总总结相关问题及解决方案,帮助大家快速定位和解决问题。

问题一:参数特殊符号被后端转义

WEB开发时,前端通过get/post方法传递参数的时,如果实参附带特殊符号,后端接收到的值中特殊符号就会被转义。

例如请求: http://localhost:10001/demo/index?name=张三(1)

后端接收到的name值中“(”和“)”被转义。

针对此问题有以下解决方案:

1、检查web.xml里是否配置了过滤特殊字符的filter,若不需要可以关掉此filter。

2、java中可以使用org.apache.commons.lang包中的public static String unescapeHtml(String str)方法来进行解码。实践中尝试了很多方法没有解决,最后使用了该方法,将接收到的包含特殊字符的字符串通过该方法进行解码。

3、在Controller接收的参数前加上@RequestBody注解,示例如下:

@PostMapping(value = "/add")
@ResponseBody
public String addMessage(@RequestBody ParamVo params) {
}

通常情况下,基于RESTful的API经常使用@RequestBody来自动绑定body中的请求参数到实体类对象。使用@RequestBody能解决大多数情况的问题,但某些特殊字符依旧无法正常解决,还需要通过方案二进行补充解决。

使用该中方案进行数据交互时,前度对应的请求需要 声明dataType和contentType,传递的参数并用JSON.stringify()转为json字符串。

$.ajax({
      url: CONTEXTPATH + "/add",
      type: 'POST',
      dataType: 'JSON',
      contentType : 'application/json',
      data: JSON.stringify(Data),
      success: function (data) {
   }
 })

问题二:/被转义成%2F导致400错误

前端GET请求url中带有路径参数,参数中有/特殊字符,前端已经转义成了%2F,后端springboot并没有收到这个请求,直接返回了400的错误。

原因:据说是tomcat默认是不支持转义的,需要手动设置一下转化,搜索tomcat的设置可以找到,但在springboot中内置的tomcat,在yml中找不到相关的配置。

解决方案:修改启动类,添加系统参数并重写WebMvcConfigurerAdapter的configurePathMatch方法。

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception {
        System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

其中实现WebMvcConfigurerAdapter接口新版本中改为实现WebMvcConfigurer接口。重写的方法名称是一样的。该部分也可以单独在WebMvc的配置类中实现,而不是放在启动类。

当然,设置tomcat的参数是需要写在main方法当中进行设置的。

问题三:整合jackson的转义

Spring Boot默认配置json转换工具就是Jackson, 如果你此时使用的正是Jackson框架,那么可在配置文件中进行是否转义的配置,配置项如下:

#jackson
#日期格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#spring.jackson.date-format=yyyy-MM-dd
#格式化输出 
spring.jackson.serialization.indent_output=true
#忽略无法转换的对象
spring.jackson.serialization.fail_on_empty_beans=false
#设置空如何序列化
spring.jackson.defaultPropertyInclusion=NON_EMPTY
#允许对象忽略json中不存在的属性
spring.jackson.deserialization.fail_on_unknown_properties=false
#允许出现特殊字符和转义符
spring.jackson.parser.allow_unquoted_control_chars=true
#允许出现单引号
spring.jackson.parser.allow_single_quotes=true

其中重点关注allow_unquoted_control_chars项的配置。

当然,相应的配置如果是在配置类中实现的自定义ObjectMapper,可以在自定义时进行设置:

@Configuration
public class JacksonConfig {
 
	@Bean
	@Primary
	@ConditionalOnMissingBean(ObjectMapper.class)
	public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();
 
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
 
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
			@Override
			public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
					throws IOException {
				jsonGenerator.writeString("");
			}
		});
		return objectMapper;
	}
 
}

在类中做过修改中, 配置文件中的配置将不再起作用。

问题四:接收JSON时发生转义字符绑定对象失败

在做Spring boot 项目时发生json转义字符绑定对象失败,原因是json里面有些字段包括空格,反斜杠等,如果框架没有对这些json进行转化时,就会报类似如下错误
org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value
at [Source: java.io.StringReader@10cfc2e3; line: 1, column: 2461]

解决办法:

1、pom.xml文件增加fastjson依赖。

 <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.15</version>
 </dependency>

如果项目中已经引入则无需新增该依赖。

2、增加配置类

@SpringBootApplication
public class JsonController extends WebMvcConfigurerAdapter {

      @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);

            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.PrettyFormat
            );
            fastConverter.setFastJsonConfig(fastJsonConfig);

            converters.add(fastConverter);
        }

}

这里的配置方法同问题二中的一样,新版本Spring Boot 通过WebMvcConfigurer接口来完成。



spring/springboot特殊参数转义处理解决方案汇总插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://www.choupangxia.com/2020/08/07/spring-springboot/