From f15cf82f1148e4b85da254cc987abbf7a52fb143 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 4 Jan 2016 00:09:50 +0100 Subject: [PATCH] Spring REST: Springfox (Swagger) --- SpringJava/REST/README | 9 ++++ SpringJava/REST/pom.xml | 53 ++++++++++++++++++++++ .../rest/doc/Swagger2Configuration.java | 49 ++++++++++++++++++++ .../spring-configuration/spring-config.xml | 27 +++++++++++ .../de/spring/webservices/rest/CarController.java | 22 +++++++++ .../spring-configuration/mvc/rest/rest-config.xml | 2 +- 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 SpringJava/REST/README create mode 100644 SpringJava/REST/src/doc/main/java/de/spring/webservices/rest/doc/Swagger2Configuration.java create mode 100644 SpringJava/REST/src/doc/main/resources/spring-configuration/spring-config.xml diff --git a/SpringJava/REST/README b/SpringJava/REST/README new file mode 100644 index 0000000..b8337ce --- /dev/null +++ b/SpringJava/REST/README @@ -0,0 +1,9 @@ + +Release: +mvn clean install + +Documentation: +mvn clean install -Dmaven.test.skip=true -Pdocumentation + +Javadoc and Sources: +mvn dependency:sources && mvn dependency:resolve -Dclassifier=javadoc diff --git a/SpringJava/REST/pom.xml b/SpringJava/REST/pom.xml index 55c725b..73e0eb3 100644 --- a/SpringJava/REST/pom.xml +++ b/SpringJava/REST/pom.xml @@ -33,6 +33,59 @@ true + + documentation + + documentation + + + false + + + + + io.springfox + springfox-swagger2 + 2.3.0 + + + io.springfox + springfox-swagger-ui + 2.3.0 + + + + + + ${basedir}/src/doc/main/resources/ + + **/*.* + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.10 + + + add-source + process-sources + + add-source + + + + src/doc/main/java/ + + + + + + + + diff --git a/SpringJava/REST/src/doc/main/java/de/spring/webservices/rest/doc/Swagger2Configuration.java b/SpringJava/REST/src/doc/main/java/de/spring/webservices/rest/doc/Swagger2Configuration.java new file mode 100644 index 0000000..98821a6 --- /dev/null +++ b/SpringJava/REST/src/doc/main/java/de/spring/webservices/rest/doc/Swagger2Configuration.java @@ -0,0 +1,49 @@ +package de.spring.webservices.rest.doc; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; + +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger.web.UiConfiguration; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class Swagger2Configuration { + private static final String DOCKET_ID = "web-services-spring-rest"; + + @Bean + public Docket documentation() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName(DOCKET_ID) + .select() + .apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class)) + .paths(PathSelectors.any()) + .build() + .pathMapping("/") + .useDefaultResponseMessages(false) + .apiInfo(metadata()); + } + + @Bean + UiConfiguration uiConfig() { + return UiConfiguration.DEFAULT; + } + + + private static ApiInfo metadata() { + return new ApiInfoBuilder() + .title("gumartinm REST API") + .description("Gustavo Martin Morcuende") + .version("1.0-SNAPSHOT") + .contact("gumartinm.name") + .build(); + } + +} diff --git a/SpringJava/REST/src/doc/main/resources/spring-configuration/spring-config.xml b/SpringJava/REST/src/doc/main/resources/spring-configuration/spring-config.xml new file mode 100644 index 0000000..68a47a4 --- /dev/null +++ b/SpringJava/REST/src/doc/main/resources/spring-configuration/spring-config.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/SpringJava/REST/src/main/java/de/spring/webservices/rest/CarController.java b/SpringJava/REST/src/main/java/de/spring/webservices/rest/CarController.java index e566d91..ceafb42 100644 --- a/SpringJava/REST/src/main/java/de/spring/webservices/rest/CarController.java +++ b/SpringJava/REST/src/main/java/de/spring/webservices/rest/CarController.java @@ -20,6 +20,10 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +//import io.swagger.annotations.ApiOperation; +//import io.swagger.annotations.ApiResponse; +//import io.swagger.annotations.ApiResponses; + @RestController @RequestMapping("/api/cars/") public class CarController { @@ -28,6 +32,12 @@ public class CarController { private final AtomicLong counter = new AtomicLong(); +// Do I want to release with Swagger dependencies? +// @ApiOperation(value = "getCars", nickname = "getAllCars", response = Car.class) +// @ApiResponses({ +// @ApiResponse(code = 404, message ="Not found"), +// @ApiResponse(code = 400, message ="Invalid input") +// }) @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public List cars() { @@ -39,6 +49,12 @@ public class CarController { return cars; } +// Do I want to release with Swagger dependencies? +// @ApiOperation(value = "getCar", nickname = "getsOneCar", response = Car.class) +// @ApiResponses({ +// @ApiResponse(code = 404, message ="Not found"), +// @ApiResponse(code = 400, message ="Invalid input") +// }) @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader, @@ -74,6 +90,12 @@ public class CarController { return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id)); } +// Do I want to release with Swagger dependencies? +// @ApiOperation(value = "postCat", nickname = "createsNewCar", response = Car.class) +// @ApiResponses({ +// @ApiResponse(code = 404, message ="Not found"), +// @ApiResponse(code = 400, message ="Invalid input") +// }) @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST) public ResponseEntity create(@RequestBody Car car) { diff --git a/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml index 551f926..420f9e5 100644 --- a/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml +++ b/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -68,5 +68,5 @@ - + -- 2.1.4