Jackson's object mapper not able to serialise sun.security.validator.ValidatorException

279 Views Asked by At

I am using Jackson's object mapper (Java 17) to serialise the exception object before logging it. The use case is to print the logs in json format. However while doing that, I got an error

om.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `sun.security.validator.ValidatorException`: Failed to construct BeanSerializer for [simple type, class sun.security.validator.ValidatorException]: (java.lang.IllegalArgumentException) Failed to call `setAccess()` on Method 'getErrorType' (of class `sun.security.validator.ValidatorException`) due to `java.lang.reflect.InaccessibleObjectException`, problem: Unable to make public java.lang.Object sun.security.validator.ValidatorException.getErrorType() accessible: module java.base does not "exports sun.security.validator" to unnamed module @61baa894 (through reference chain: java.util.LinkedHashMap["EXCEPTION"]->javax.net.ssl.SSLHandshakeException["cause"])

From what i understand, ObjectMapper while trying to serialise tried accessing the public getter method ('getErrorType') of ValidatorException, but failed to do so and hence the error. Does this class come under some restricted set of files/classes that can't be used elsewhere?

I am pasting a similar code i have for logging. The code doesn't reproduce the original error, because the ValidatorException is not accessible to me. Because of this reason only, the original error might be coming. But help me out here if I am missing something.

package com.wm.dp.ous.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.Map;


@Slf4j
public class Main {

  private static final ObjectMapper OBJECT_MAPPER =
          new ObjectMapper()
                  .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                  .registerModule(new JavaTimeModule());
  
  public static void main(String[] args) throws JsonProcessingException {

    Map<String, Object> fields = new LinkedHashMap<>();
/*   Assume that we have an exception object here, for some Reason, I am not able to use ValidatorException class in
     code. (the same could be the reason behind my error) But using some other exception class for explanation.
     Also note this code doesn't produce the original error that I am facing*/
    RuntimeException exception = new RuntimeException("Test exception");
    fields.put("MESSAGE", "Unexpected error occurred");
    fields.put("EXCEPTION", exception);
    log.info(getJsonString(fields));
  }

  private static String getJsonString(Object obj) {
    try {
      return OBJECT_MAPPER.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
      log.error("Error while doing Json Logging", e);
      return "";
    }
  }

}

I am planning on adding these configurations on my objectMapper. But i think these would not work. Since the first one works, if the object doesn't have anything to print. (Empty) And the second is to access private fields without getters, which is not the case in my example. ('gotErrorType' is already a public function)

          .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
          .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
0

There are 0 best solutions below