Giter Club home page Giter Club logo

springfox-gson-example's Introduction

Build Status

Spring Boot with SpringFox and Gson Example

This is an example on how to use SpringFox with Gson. SpringFox doesn't work with Gson out of the box.

To make Springfox work with Gson, you have to do the following:

Include Gson JAR

Include Gson JAR in your Maven pom.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

Don't Remove Jackson Databind JAR

You will still need jackson-databind JAR. If you are missing the JAR, your application will make the following complain:

org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'objectMapperConfigurer' defined in 
springfox.documentation.spring.web.SpringfoxWebMvcConfiguration: 
Post-processing of merged bean definition failed; 
nested exception is java.lang.NoClassDefFoundError: 
com/fasterxml/jackson/databind/ObjectMapper

The exception is due to springfox.documentation.spring.web.ObjectMapperConfigurer complaining about missing com.fasterxml.jackson.databind.ObjectMapper.

The ObjectMapper exists in com.fasterxml.jackson.core:jackson-databind:jar

Register JSON Serializers

Register custom serializers for different Springfox artifacts as shown below. This example includes the custom serializers.

Gson gson = GsonBuilder()
    // needed for making calls to /v2/api-docs
    .registerTypeAdapter(Json.class, 
        new SpringfoxJsonSerializer())
    // rest are needed for making calls to /swagger-ui.html
    .registerTypeAdapter(ApiListing.class, 
        new SpringfoxApiListingJsonSerializer())
    .registerTypeAdapter(SwaggerResource.class, 
        new SpringfoxResourceJsonSerializer())
    .registerTypeAdapter(ResourceListing.class, 
        new SpringfoxResourceListingJsonSerializer())
    .registerTypeAdapter(UiConfiguration.class, 
        new SpringfoxUiConfigurationJsonSerializer())
    // needed if you have security
    .registerTypeAdapter(SecurityConfiguration.class, 
        new SpringfoxSecurityConfigurationJsonSerializer())
    ...
    .create();

The serializer for springfox.documentation.spring.web.json.Json is needed for viewing Swagger API JSON docs at '/v2/api-docs'.

Rest of the serializers are needed for viewing Swagger UI at /swagger-ui.html

Build

To build the JAR, execute the following command from the parent directory:

mvn clean install

Run

To run the application fromm command line,

java -jar target/springfox-gson-example-1.0.0.jar
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

2017-11-23 17:14:35.736  INFO 9896 --- [           main] com.basaki.Application                   : Starting Application on jdoe-78814.local with PID 9896 (/Users/jdoe/springfox-gson-example/target/springfox-gson-example-1.0.0.jar started by indra.basak in /Users/jdoe/springfox-gson-example)
...
2017-11-23 17:14:42.187  INFO 9896 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2017-11-23 17:14:42.455  INFO 9896 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-11-23 17:14:42.463  INFO 9896 --- [           main] com.basaki.Application                   : Started Application in 7.371 seconds (JVM running for 7.867)

Access Swagger Endpoints

Swagger UI

You can view the Swagger UI at http://localhost:8080/swagger-ui.html.

Swagger JSON

You can view Swagger JSOn doc at http://localhost:8080/v2/api-docs?group=gson

springfox-gson-example's People

Contributors

indrabasak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.