1

refine swagger example

This commit is contained in:
Christoph
2020-03-07 17:21:17 +01:00
parent 1a3c0a459f
commit 6d32092dcf
6 changed files with 83 additions and 36 deletions

View File

@ -3,11 +3,16 @@ package mops.gruppen2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Collections;
@SpringBootApplication
@EnableSwagger2
public class Gruppen2Application {
@ -17,7 +22,24 @@ public class Gruppen2Application {
@Bean
public Docket productAPI() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("mops.gruppen2")).build();
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/products/**"))
.apis(RequestHandlerSelectors.basePackage("mops.gruppen2"))
.build()
.apiInfo(apiMetadata());
}
private ApiInfo apiMetadata() {
return new ApiInfo(
"Gruppenbildung API",
"API zum anfragen/aktualisieren der Gruppendaten.",
"0.0.1",
"Free to use",
new Contact("gruppen2", "https://github.com/hhu-propra2/abschlussprojekt-it-bois", ""),
"",
"",
Collections.emptyList()
);
}
}

View File

@ -1,31 +0,0 @@
package mops.gruppen2.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SwaggerAPIController {
/**
* Ein Beispiel für eine API mit Swagger.
*
* @return Eine Liste von Produkten, repräsentiert durch Strings
*/
@RequestMapping(value = "/products", method = RequestMethod.GET)
public List<String> getProducts() {
List<String> productList = new ArrayList<>();
productList.add("Honey");
productList.add("Almond");
return productList;
}
@RequestMapping(value = "/products", method = RequestMethod.POST)
public String createProduct() {
return "Product is saved successfully";
}
}

View File

@ -0,0 +1,47 @@
package mops.gruppen2.controller;
import com.github.javafaker.Faker;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mops.gruppen2.domain.ProductSwaggerExample;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* Ein Beispiel für eine API mit Swagger.
*/
@RestController
@RequestMapping("/products")
public class SwaggerAPIControllerExample {
private final Faker faker = new Faker();
private final List<ProductSwaggerExample> products = new ArrayList<>();
@GetMapping("/get/all")
@ApiOperation(value = "Erzeugt eine Liste mit allen gespeicherten Produkten")
public List<ProductSwaggerExample> getProducts() {
return products;
}
@GetMapping("/get/{index}")
public ProductSwaggerExample getProduct(@ApiParam("Produkt Index") @PathVariable int index) {
return products.get(index);
}
@PostMapping("/save")
public String saveProduct(@RequestBody ProductSwaggerExample product) {
products.add(product);
return "Product saved successfully";
}
@PostMapping("/random")
public String saveRandomProduct() {
products.add(new ProductSwaggerExample(faker.food().ingredient(), "Empty"));
return "Product saved successfully";
}
}

View File

@ -0,0 +1,12 @@
package mops.gruppen2.domain;
import lombok.Value;
// @ApiModelProperty
@Value
public class ProductSwaggerExample {
// @ApiModelProperty
String name;
String description;
}

View File

@ -74,8 +74,6 @@ class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
http.headers().frameOptions().disable();
}
/**
* Declaring this class enables us to use the Spring specific
* {@link org.springframework.security.access.annotation.Secured} annotation