Skip to content

GSON problem: custom adapters not used #36

@sergiomichels

Description

@sergiomichels

For some reason GSON is failing in get the GsonBuilder from the applicationContext, this causes the class to return a new GsonBuilder() without my custom Adapters. For example:

@Lazy
private GsonBuilder gsonBuilder = {
  GsonBuilder gsonBuilder = applicationContext?.getBean('gsonBuilder', GsonBuilder)
  if(gsonBuilder) {
    println "**** Using applicationContext GsonBuilder"
  } else {
    println "**** Not found bean gsonBuilder"
    gsonBuilder = new GsonBuilder()
  }
  return gsonBuilder
}()

In my case it prints "Not found bean gsonBuilder" and by consequence using render model as GSON fails to use my adapters.

The problem goes away if I explicity declare gsonBuilder in my controller and use render contentType: 'application/json', text: gson.toJson(model)

Maybe it's a problem with ApplicationContextAware? I can also see that applicationContext is null in GSON.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions