Search code examples
micronaut

Micronaut convert array of objects to json response in Controller method


I have array of objects in Micronaut, want to return as JSON response of controller method. what is the best way of converting objects into json response.

In Groovy I do it like this array of server objects:

def generator = new JsonGenerator.Options()
        .excludeNulls()
        .dateFormat('yyyy@MM')
        .excludeFieldsByName('age', 'password')
        .excludeFieldsByType(URL)
        .build()

def output = JsonOutput.prettyPrint(generator.toJson(servers))

Same is not compiling in Micronaut project.

Thanks SR


Solution

  • Same is not compiling in Micronaut project.

    I have pasted the code directly from the question above into a Micronaut app and it compiles fine.

    Make sure you are using groovy.json.JsonGenerator, not com.fasterxml.jackson.core.JsonGenerator. groovy.json.JsonGenerator is in 'org.codehaus.groovy:groovy-json'.