com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value异常

springboot对象返回,一直报生成json异常,经过检查,发现是自己在做xss防护时对出参进行了json的处理(copy代码不可取,囧)

异常信息

com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value异常_第1张图片

 

 这里进行了出参处理了,但实际上只要对入参处理就行了,把这个类改成入参处理即可

public class XssStringJsonSerializer extends JsonSerializer {
    @Override
    public Class handledType() {
        return String.class;
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (s == null) {
            String encodedValue = StringEscapeUtils.escapeHtml4(s);
            jsonGenerator.writeString(encodedValue);
        }
    }

//入参检查
public class XssStringJsonSerializer extends JsonDeserializer {
    public XssStringJsonSerializer(Class string) {
        super();
    }

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException
    {
        String value = jsonParser.getValueAsString();
        if (value != null){
            return StringEscapeUtils.escapeHtml4(value.toString());
        }
        return value;
    }

    @Override
    public Class handledType() {
        return String.class;
    }
}

耽误了一小时代码排除,xss防护copy别人代码的。。。没理解就用的下场。。

你可能感兴趣的:(com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value异常)