我希望在不更改自动搭建的控制器的情况下使用JACKSON而不是grails JSON。
是否可以将当前的grails JSON转换器无缝替换为另一个?
是否有任何类可以实现其他的AbstractConverter ...
答案 0 :(得分:0)
应该很简单。 AbstractConverter是您需要扩展的所有内容:
render result as JackSON
像这样的东西***: ***取自这里:https://github.com/sjhorn/grails-jackson/blob/master/src/groovy/com/hornmicro/JackSON.groovy
class JackSON extends AbstractConverter {
Object target
public JackSON() {
}
public JackSON(Object target) {
this()
setTarget(target)
}
public void render(Writer out) throws ConverterException {
try {
ObjectMapper mapper = new ObjectMapper()
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
mapper.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false)
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false)
mapper.writeValue(out, target)
} catch(e) {
throw new ConverterException(e)
}
try {
out.flush()
out.close()
} catch (Exception e) {
log.warn("Unexpected exception while closing a writer: " + e.getMessage())
}
}
public void render(HttpServletResponse response) throws ConverterException {
response.setContentType(GrailsWebUtil.getContentType("application/json", "UTF-8"));
try {
render(response.getWriter())
} catch (IOException e) {
throw new ConverterException(e)
}
}
public Object getWriter() throws ConverterException {
throw new ConverterException("Not Implemented")
}
public void convertAnother(Object o) throws ConverterException {
throw new ConverterException("Not Implemented")
}
public void build(Closure c) throws ConverterException {
throw new ConverterException("Not Implemented")
}
public ObjectMarshaller lookupObjectMarshaller(Object target) {
return null
}
public void setTarget(Object target) {
this.target = target
}
}