From 4618f0d773fbf915f896383ffd5b8d0cd3b0e761 Mon Sep 17 00:00:00 2001 From: Carlos Cavero Date: Fri, 27 Dec 2019 23:08:50 +0100 Subject: [PATCH] Bael 3618 array strings body Swagger API mvc-2 (#8421) * Add springfox, devtools and apache commons dependencies * Add the Swagger yaml file to the main project * Add the general README and Swagger yaml description * Implement the functionalities and add the Swagger annotations * Adjust indentation and reformatting * Change formatting of pom from 6 to 4 --- spring-boot-mvc-2/README.md | 1 + spring-boot-mvc-2/pom.xml | 77 ++++++++++++++++++- .../SpringBootSwaggerApplication.java | 13 ++++ .../swagger2boot/config/Swagger2Config.java | 34 ++++++++ .../controller/FooController.java | 35 +++++++++ .../com/baeldung/swagger2boot/model/Foo.java | 44 +++++++++++ .../main/resources/swagger-description.yml | 33 ++++++++ 7 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java create mode 100644 spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java create mode 100644 spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java create mode 100644 spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/Foo.java create mode 100644 spring-boot-mvc-2/src/main/resources/swagger-description.yml diff --git a/spring-boot-mvc-2/README.md b/spring-boot-mvc-2/README.md index 0ff0f1f156..81a969bf87 100644 --- a/spring-boot-mvc-2/README.md +++ b/spring-boot-mvc-2/README.md @@ -5,4 +5,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects. ### Relevant Articles: - [Functional Controllers in Spring MVC](https://www.baeldung.com/spring-mvc-functional-controllers) +- [Specify an array of strings as body parameter in Swagger API](https://www.baeldung.com/array-of-strings-as-body-parameter-in-swagger-api) - More articles: [[prev -->]](/spring-boot-mvc) diff --git a/spring-boot-mvc-2/pom.xml b/spring-boot-mvc-2/pom.xml index fcc0fc8b82..0f5a4bcd77 100644 --- a/spring-boot-mvc-2/pom.xml +++ b/spring-boot-mvc-2/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-mvc-2 spring-boot-mvc-2 @@ -12,7 +12,7 @@ org.springframework.boot spring-boot-starter-parent 2.2.0.BUILD-SNAPSHOT - + @@ -20,6 +20,36 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + true + + + + + io.springfox + springfox-swagger2 + ${spring.fox.version} + + + + io.springfox + springfox-swagger-ui + ${spring.fox.version} + + + + io.springfox + springfox-spring-webmvc + ${spring.fox.version} + + + + org.apache.commons + commons-lang3 + @@ -28,11 +58,50 @@ org.springframework.boot spring-boot-maven-plugin - com.baeldung.springbootmvc.SpringBootMvcFnApplication + ${start-class} JAR + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + + + + 3.0.0-SNAPSHOT + com.baeldung.swagger2boot.SpringBootSwaggerApplication + + + \ No newline at end of file diff --git a/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java new file mode 100644 index 0000000000..0f68f75d16 --- /dev/null +++ b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swagger2boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootSwaggerApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootSwaggerApplication.class, args); + } + +} diff --git a/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java new file mode 100644 index 0000000000..a0048eb505 --- /dev/null +++ b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java @@ -0,0 +1,34 @@ +package com.baeldung.swagger2boot.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +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.swagger2.annotations.EnableSwagger2WebMvc; + +@Configuration +@EnableSwagger2WebMvc +public class Swagger2Config { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).select() + .apis(RequestHandlerSelectors.basePackage("com.baeldung.swagger2boot.controller")) + .paths(PathSelectors.regex("/.*")) + .build() + .apiInfo(apiEndPointsInfo()); + } + + private ApiInfo apiEndPointsInfo() { + return new ApiInfoBuilder().title("Swagger Array") + .description("This is a sample Swagger description for an Array server") + .license("Apache 2.0") + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") + .version("1.0.0") + .build(); + } +} \ No newline at end of file diff --git a/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java new file mode 100644 index 0000000000..ac6d99c40c --- /dev/null +++ b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java @@ -0,0 +1,35 @@ +package com.baeldung.swagger2boot.controller; + +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import com.baeldung.swagger2boot.model.Foo; + +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; + +@Controller +public class FooController { + + public FooController() { + super(); + } + + // API - write + @RequestMapping(method = RequestMethod.POST, value = "/foos") + @ResponseStatus(HttpStatus.CREATED) + @ResponseBody + @ApiImplicitParams({ @ApiImplicitParam(name = "foo", value = "List of strings", paramType = "body", dataType = "Foo") }) + public Foo create(@RequestBody final Foo foo) { + foo.setId(Long.parseLong(randomNumeric(2))); + return foo; + } + +} diff --git a/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/Foo.java b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/Foo.java new file mode 100644 index 0000000000..e48c2016c0 --- /dev/null +++ b/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/Foo.java @@ -0,0 +1,44 @@ +package com.baeldung.swagger2boot.model; + +import java.util.List; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel +public class Foo { + private long id; + + @ApiModelProperty(name = "name", dataType = "List", example = "[\"str1\", \"str2\", \"str3\"]") + private List name; + + public Foo() { + super(); + } + + public Foo(final long id, final List name) { + super(); + + this.id = id; + this.name = name; + } + + // + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public List getName() { + return name; + } + + public void setName(final List name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/spring-boot-mvc-2/src/main/resources/swagger-description.yml b/spring-boot-mvc-2/src/main/resources/swagger-description.yml new file mode 100644 index 0000000000..b40ddc80f0 --- /dev/null +++ b/spring-boot-mvc-2/src/main/resources/swagger-description.yml @@ -0,0 +1,33 @@ +swagger: "2.0" +info: + description: "This is a sample Swagger description for an Array server" + version: "1.0.0" + title: "Swagger Array" + license: + name: "Apache 2.0" + url: "http://www.apache.org/licenses/LICENSE-2.0.html" +basePath: "/localhost:8080/" +tags: +- name: "foo-controller" + description: "Foo controller" + +paths: + /foos: + post: + tags: + - "foo-controller" + summary: "create" + description: "" + parameters: + - in: body + description: "" + required: true + name: name + schema: + type: array + items: + type: string + example: ["str1", "str2", "str3"] + responses: + default: + description: OK \ No newline at end of file