fastjson 最近几个新版本会把括号序列化成unicode字符

w41d8nur  于 2个月前  发布在  其他
关注(0)|答案(9)|浏览(37)

hi,
如题

nwnhqdif

nwnhqdif1#

提供下版本和测试用例吧.我测试没发现问题

ui7jx7zq

ui7jx7zq2#

不好意思,当时比较赶,没写详细,现补充一下:
所用版本:
fastjson: 1.2.56
spring mvc: 4.3.22

spring mvc配置:

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>application/json;charset=UTF-8</value>
				</list>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

使用:

@RestController(value = "testController")
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Map<String, Object> test() {
        Map<String, Object> map = new HashMap<>();
        map.put("a", "()");
        return map;
    }
}

访问打印出来的是:

{"a":"\u0028\u0029"}

而用 fastjson-1.2.29 打印出来的是:

{"a":"()"}

o0lyfsai

o0lyfsai3#

我这边也出现了,版本也是1.2.56

cgyqldqp

cgyqldqp4#

升级到1.2.58也是

k10s72fa

k10s72fa5#

从1.2.36版本,“在spring-mvc场景,缺省打开增强Feature.BrowserSecure选项,防御xss安全攻击”,开发出现

3npbholx

3npbholx6#

从1.2.36版本,“在spring-mvc场景,缺省打开增强Feature.BrowserSecure选项,防御xss安全攻击”,开发出现
@linminqin 你是对的。
打开了SerializerFeature.BrowserSecure选项,会将()<>等字符转换为16进制表示。

public void test_4() throws Exception {
        String text = JSON.toJSONString("(", SerializerFeature.BrowserSecure);
        System.out.println(text);
        assertEquals("\"\\u0028\"", text);
    }

    public void test_5() throws Exception {
        String text = JSON.toJSONString(")", SerializerFeature.BrowserSecure);
        assertEquals("\"\\u0029\"", text);
    }

@jakeszhong 如果想显示原符号,可以禁用此选项。如下是通过编程方式配置SpringMVC来取消SerializerFeature.BrowserSecure功能的demo,亲测可行。

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //自定义配置...
        FastJsonConfig config = new FastJsonConfig();
        SerializerFeature.config(SerializerFeature.of(config.getSerializerFeatures()), SerializerFeature.BrowserSecure, false);
        config.setSerializerFeatures(SerializerFeature.values());
        converter.setFastJsonConfig(config);
        converters.add(0, converter);
    }
eoxn13cs

eoxn13cs7#

需要加入这个功能 SerializerFeature.BrowserSecure 才可以,不然会影响序列化

kq4fsx7k

kq4fsx7k8#

请问在MVC的配置文件中如何进行配置呢

6ovsh4lw

6ovsh4lw9#

您好,来信已收到,我会尽快给您回复。                                                                                            chenyujun

相关问题