Solution to Swagger2 failure when Spring MVC uses FastJsonHttpMessageConverter

Source: Internet
Author: User
Tags json object object serialization


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.