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
This commit is contained in:
parent
8fab22b6e7
commit
4618f0d773
@ -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)
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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">
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-mvc-2</artifactId>
|
||||
<name>spring-boot-mvc-2</name>
|
||||
@ -12,7 +12,7 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -20,6 +20,36 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Swagger dependencies for REST documentation -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-spring-webmvc</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -28,11 +58,50 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.springbootmvc.SpringBootMvcFnApplication</mainClass>
|
||||
<mainClass>${start-class}</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<spring.fox.version>3.0.0-SNAPSHOT</spring.fox.version>
|
||||
<start-class>com.baeldung.swagger2boot.SpringBootSwaggerApplication</start-class>
|
||||
<!-- <start-class>com.baeldung.springbootmvc.SpringBootMvcFnApplication</start-class> -->
|
||||
</properties>
|
||||
|
||||
</project>
|
13
spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java
Normal file
13
spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/SpringBootSwaggerApplication.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
35
spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java
Normal file
35
spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/FooController.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<String> name;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(final long id, final List<String> name) {
|
||||
super();
|
||||
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final List<String> name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
33
spring-boot-mvc-2/src/main/resources/swagger-description.yml
Normal file
33
spring-boot-mvc-2/src/main/resources/swagger-description.yml
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user