java使用fastjson转化失败:Exception in thread “xx“ com.alibaba.fastjson.JSONException: set property error

错误信息:

Exception in thread "main" com.alibaba.fastjson.JSONException: set property error, schema
	at com.alibaba.fastjson.parser.deserializer.FieldDeserializer.setValue(FieldDeserializer.java:162)
	at com.alibaba.fastjson.parser.DefaultJSONParser.handleResovleTask(DefaultJSONParser.java:1543)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:367)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
	at com.example.demo.controller.SwaggerOutPutBController.main(SwaggerOutPutBController.java:147)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.alibaba.fastjson.parser.deserializer.FieldDeserializer.setValue(FieldDeserializer.java:110)
	... 5 more
Disconnected from the target VM, address: '127.0.0.1:52685', transport: 'socket'

Process finished with exit code 1

解决方案:

我是慢慢打断点,然后找到转化失败的那个键(这里已经提示我了,是schema这个键)

然后找到错误的那个值,把那个键给替换成自己能用的。

    public static void main(String[] args) {
        String s = "{\n" +
                "                \"in\":\"body\",\n" +
                "                \"name\":\"param\",\n" +
                "                \"description\":\"param\",\n" +
                "                \"required\":true,\n" +
                "                \"schema\":{\n" +
                "                    \"$ref\":\"#/definitions/ApiEditParam\"\n" +
                "                }\n" +
                "            }";
        MyParameter mySchema = JSON.parseObject(s.replaceAll(Matcher.quoteReplacement("¥ref"), "ref"), MyParameter.class);//替换,这里用Matcher.quoteReplacement 因为$在replaceAll会识别错误
        System.out.println(mySchema);
    }

Ps:我的实体类(2个)


//类1
@Data
public class MyParameter implements Serializable {

    private String name;//参数名

    private String description;//参数描述

    private String in;//参数方式:query,form-data

    private Boolean required;//参数是否必传

    private MySchema schema;//参数描述
}


//类2
@Data
public class MySchema implements Serializable {

    private String type;//参数属性

    private String ref;

}

你可能感兴趣的:(Java,java,开发语言)