How to use java expression in spring data projection using SpEL

234 views Asked by At

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?

0

There are 0 answers