This solution has been merged into fastjson 1.2.15. Please use version 1.2.15 + without the following modifications.
FastJson is Alibaba's open-source high-performance JSON conversion tool. When Spring MVC is used for JSON conversion, FastJsonHttpMessageConverter provided by FastJson is usually used. But after we use Swagger2 in the project, our Api documentation will not work (although the swagger-ui interface can be displayed, there is no api content, and by accessing/v2/api-docse, we get the return time {} instead of the content configured in the previous document.
After the fastjsonsource code is downloaded, the single-step transformation of serializerto springfox.doc umentation. spring. web. json. Json can solve the problem {}.
The following describes how to solve this problem.
Update fastjson
Fastjson is updated to version 1.2.10 or later. In earlier versions, FastJsonHttpMessageConverter does not expose the Serializer configuration of fastjson. After the update, we can easily add serialization support for springfox.doc umentation. spring. web. json. Json. json.
This article uses 1.2.12 as an example:
<Dependency>
<GroupId> com. alibaba </groupId>
<ArtifactId> fastjson </artifactId>
<Version> 1.2.12 </version>
</Dependency>
Implement custom serialization class
Implement the ObjectSerializer and ObjectDeserializer interfaces to define the serialization implementation and deserialization implementation of the target respectively. Here, we only need to re-write the writeexample, and the actual sequence process will return the json document in the springfox.doc umentation. spring. web. Json. json object correctly.
Public class Json {
Private final String value;
Public Json (String value ){
This. value = value;
}
@ JsonValue
@ JsonRawValue
Public String value (){
Return this. value;
}
}
Through springfox.doc umentation. spring. web. json. Json source code and one-step debugging, you can see that the returned content required by swagger-ui is provided by the value field in the Json object. Therefore, we only need to output the value of the Json object in the write method, as follows:
Public class SwaggerJsonSerializer implements ObjectSerializer, ObjectDeserializer {
Public final static SwaggerJsonSerializer instance = new SwaggerJsonSerializer ();
@ Override
Public void write (JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer. getWriter ();
Json json = (Json) object;
Out. write (json. value ());
}
@ Override
Public <T> T deserialze (DefaultJSONParser parser, Type type, Object fieldName ){
Return null;
}
@ Override
Public int getFastMatchToken (){
Return 0;
}
}
Add custom serialization class to FastJsonHttpMessageConverter
Umentation. spring. web. json. Json is converted using our own SwaggerJsonSerializer, as follows:
Public class FastJsonHttpMessageConverterEx extends FastJsonHttpMessageConverter {
Public FastJsonHttpMessageConverterEx (){
Super ();
This. getFastJsonConfig (). getSerializeConfig (). put (Json. class, SwaggerJsonSerializer. instance );
}
}
Finally, replace FastJsonHttpMessageConverterEx with the original FastJsonHttpMessageConverter in the configuration file.