JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth

java8 new out of YearMonth can be easily used to represent a particular month. The following error occurred when data was found x-www-from-urlencoded format can use the "2018-12" type of reception when I used in the project springmvc to receive YearMonth types of data, but the receiving application / json data in the post request                                              

2020-02-18 11:18:25.284 WARN 16212 --- [nio-8090-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth;

 

YearMonth the abnormality should be no public constructors, fastjson Parsing of YearMonth; fastjson case used as a tool springmvc sequence, similar to the following configuration

@Configuration
public class MyConverter implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        converters.add(0,fastJsonHttpMessageConverter);
    }
}

MonthYear did find it by looking at the privatization of the constructor, leaving only two static methods

    private YearMonth(int year, int month) {
        this.year = year;
        this.month = month;
    }
    public static YearMonth of(int year, int month) {
        YEAR.checkValidValue(year);
        MONTH_OF_YEAR.checkValidValue(month);
        return new YearMonth(year, month);
    }
    private YearMonth with(int newYear, int newMonth) {
        if (year == newYear && month == newMonth) {
            return this;
        }
        return new YearMonth(newYear, newMonth);
    }

 

 

But when can get get get request, indicating jackson should have a special deal with its tools. The springmvc sequences modified to the default tool jackson, can find application / json request "2018-12" data format successfully deserialize YearMonth type. Then found by looking jackson did with serialized YearMonth deserialization tool

 

 (It seems springmvc the jackson as a default serialization tool or a reason ..)

So the solution seems to be found. The default is to use the tool as a serialized jackson can be solved.

Of course, if the project is not good to change, you want to use fastjson have a solution. fastjson field allows for the customization tool deserialized, then the corresponding field identifies the specified class to deserialize (similar to the principle of sequence)

public class YearMonthDeserializer implements ObjectDeserializer {
    @Override
    public YearMonth deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        return YearMonth.parse(parser.getLexer().stringVal());
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
public class AddVO {

    @JSONField(deserializeUsing = YearMonthDeserializer.class)
    private YearMonth yearMonth;

    public YearMonth getYearMonth() {
        return yearMonth;
    }

    public void setYearMonth(YearMonth yearMonth) {
        this.yearMonth = yearMonth;
    }
}

 

Guess you like

Origin www.cnblogs.com/hetutu-5238/p/12325457.html