1

The projection for converting a string field to JSONObject using default method as below works fine:

public interface ResponseProjection {

    @JsonIgnore
    @Value("#{target.extraData}")
    String getExtras();

    default JSONObject getExtraData() {
        return new Gson().fromJson(getExtras(), JSONObject.class);
    }

}

whereas, while using open projection like below?

public interface ResponseProjection {

    @Value("#{java(new GsonBuilder().disableHtmlEscaping().create().fromJson(target.extraData, JSONObject.class))}")
    JSONObject getExtraData();

}

throws error:

w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: EL1003E: A problem occurred whilst attempting to construct an object of type 'GsonBuilder' using arguments '()'; nested exception is com.fasterxml.jackson.databind.JsonMappingException: EL1003E: A problem occurred whilst attempting to construct an object of type 'GsonBuilder' using arguments '()' (through reference chain: com.sun.proxy.$Proxy162["extraDataDto"]->com.sun.proxy.$Proxy166["extraData"])]

Is there a way to parse jsonString to json using Gson using open projection?

CdVr
  • 323
  • 3
  • 15
  • You might consider outsourcing the creation of the GsonBuilder to a service and then have the service method return the GsonBuilder object. You should be able to call that from your SpEL statement. Looks like Jackson just doesn't recognize the GsonBuilder constructor for whatever reason – Rhett Harrison Nov 16 '22 at 04:39

0 Answers0