[alibaba/fastjson]关于返回 json 与jsonp的兼容问题

2025-11-13 653 views
3

在springmvc 中只能配置一个有效的HttpMessageConverter的,不能同时返回 json与jsonp。请问大神们。是否已经有了解决方案?

回答

1

FastJsonpHttpMessageConverter4

8

@Override protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { HttpHeaders headers = outputMessage.getHeaders(); ByteArrayOutputStream outnew = new ByteArrayOutputStream(); int len = writePrefix(outnew, obj); Object value = obj; if (obj instanceof MappingFastJsonValue) { MappingFastJsonValue container = (MappingFastJsonValue) obj; value = container.getValue(); } len += JSON.writeJSONString(outnew, // fastJsonConfig.getCharset(), // value, // fastJsonConfig.getSerializeConfig(), // fastJsonConfig.getSerializeFilters(), // fastJsonConfig.getDateFormat(), // JSON.DEFAULT_GENERATE_FEATURE, // fastJsonConfig.getSerializerFeatures()); len += writeSuffix(outnew, obj); if (fastJsonConfig.isWriteContentLength()) { headers.setContentLength(len); } OutputStream out = outputMessage.getBody(); outnew.writeTo(out); outnew.close(); } 这里只能返回,jsonp, 通过FastResponseBodyAdvice的返回值识别,是返回json,还是jsonp,

7

@githublaohu 新版的FastJsonHttpMessageConverter是支持的,但是现在还没发布RELEASE

9

多谢,使用官方的还是稳妥点。虽然自己实现一套怕过不上官方的步伐。 还是大神犀利。