问题描述
我在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.JsonGenerator
。 groovy.json.JsonGenerator
在'org.codehaus.groovy:groovy-json'
中。