代码之家  ›  专栏  ›  技术社区  ›  Nuñito Calzada

在Java中解析Json->INCLUDE_SOURCE_in_LOCATION `已禁用

  •  0
  • Nuñito Calzada  · 技术社区  · 8 月前

    我有这个Json:

    [
        {
            "Title": "New Moon in Sagittarius",
            "eventStartDate": "01/12/2024",
            "eventEndDate": "01/12/2024"
        },
        {
            "Title": "Christmas",
            "eventStartDate": "25/12/2024",
            "eventEndDate": "25/12/2024"
        },
        {
            "Title": "New Year's Eve",
            "eventStartDate": "31/12/2024",
            "eventEndDate": "31/12/2024"
        }
    ]
    

    但当我解析它时:

    ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                objectMapper.setDateFormat(new StdDateFormat().withLenient(false));
    
               
    
                // Convert JSON string to Wrapper Class
                AstronomicalEventsWrapper wrapper = objectMapper.readValue
                        (jsonString, AstronomicalEventsWrapper.class);
    

    我有这个错误:

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.systicblanets.model.AstronomicalEventsWrapper` from Array value (token `JsonToken.START_ARRAY`)
     at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
        at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
        at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1767)
        at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1541)
        at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1488)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:656)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:211)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
        at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4899)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3846)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3814)
        at com.mysticplanets.schedulingtasks.CreateAstronomicalEventTask.sendMessage(CreateAstronomicalEventTask.java:60)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.runInternal(ScheduledMethodRunnable.java:130)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.lambda$run$2(ScheduledMethodRunnable.java:124)
        at io.micrometer.observation.Observation.observe(Observation.java:499)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:124)
        at org.springframework.scheduling.config.Task$OutcomeTrackingRunnable.run(Task.java:83)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
        at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:358)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1583)
    

    import java.util.List;
    
    public class AstronomicalEventsWrapper {
    
        private List<AstronomicalEvent> astrologicalEvents;
    
        public List<AstronomicalEvent> getAstronomicalEvents() {
            return astrologicalEvents;
        }
    
        public void setAstronomicalEvents(List<AstronomicalEvent> astrologicalEvents) {
            this.astrologicalEvents = astrologicalEvents;
        }
    
        @Override
        public String toString() {
            return "AstronomicalEventsWrapper{" +
                    "astrologicalEvents=" + astrologicalEvents +
                    '}';
        }
    }
    
    1 回复  |  直到 8 月前
        1
  •  2
  •   Sharon Ben Asher    8 月前

    根据 Jackson documentation , INCLUDE_SOURCE_IN_LOCATION 在生成异常堆栈跟踪时使用,因此它与实际的解析错误无关。

    我认为解析错误是因为输入是一个对象数组,因此反序列化的目标必须是数组或集合。即使你的包装器包含一个集合,它仍然是一个对象,输入必须是对象,这意味着从开始 '{'