Micronaut在Controller方法中将对象数组转换为json响应

问题描述

我在Micronaut中有对象数组,想要返回为控制器方法的JSON响应。将对象转换为json响应的最佳方法是什么。

在Groovy中,我像这样处理服务器对象数组:

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

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

Same尚未在Micronaut项目中编译。

谢谢 SR

解决方法

Same尚未在Micronaut项目中编译。

我已将上面问题中的代码直接粘贴到Micronaut应用程序中,并且可以正常编译。

确保使用的是groovy.json.JsonGenerator,而不是com.fasterxml.jackson.core.JsonGeneratorgroovy.json.JsonGenerator'org.codehaus.groovy:groovy-json'中。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...