springfox(swagger2) does not work with GsonHttpMessageConverterConfig

前端 未结 5 1886
耶瑟儿~
耶瑟儿~ 2020-12-08 17:42

What I am trying to build is a spring-boot (v1.2.3) application and expose my Rest API with SpringFox(swagger2) v2.0.0

my Swagger Spring config



        
5条回答
  •  一向
    一向 (楼主)
    2020-12-08 18:08

    A couple of things I found missing with the above instructions is the package and imports. When I first tried this, I used my own packages but swagger-ui.html still said there were no packages found. It appears the package is specific.

    The classes below are exactly the same as above, but I included the entire class with package names and imports. Registering the adapter is the same as documented above.

    First the JSON class

    package springfox.documentation.spring.web.json;
    
    import com.fasterxml.jackson.annotation.*;
    
    public class Json {
        private final String value;
    
        public Json(String value) {
        this.value = value;
        }
    
        @JsonValue
        @JsonRawValue
        public String value() {
        return value;
        }
    }
    

    and the adapter class:

    package springfox.documentation.spring.web.json;
    
    import java.lang.reflect.Type;
    
    import com.google.gson.*;
    
    public class SpringfoxJsonToGsonAdapter implements com.google.gson.JsonSerializer {
    
        @Override
        public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
        final JsonParser parser = new JsonParser();
        return parser.parse(json.value());
        }
    
    }
    

提交回复
热议问题