Skip to content

Instantly share code, notes, and snippets.

@mloza
Last active February 11, 2021 18:09
Show Gist options
  • Save mloza/09d2382794d42e89f835780a6bd52a3e to your computer and use it in GitHub Desktop.
Save mloza/09d2382794d42e89f835780a6bd52a3e to your computer and use it in GitHub Desktop.
Kod do postu o Spring Boot i Swaggerze znajdującego się pod adresem https://blog.mloza.pl/spring-boot-swagger-ui/
@Operation(
summary = "List all entries from database",
description = "Returns all entries that was saved to our database"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Entry was added to database"),
@ApiResponse(responseCode = "500", description = "Something went wrong, entry was not added")
})
@GetMapping("/api/list")
public ListEntryResponse listObjects() {
return new ListEntryResponse(database);
}
@Operation(
summary = "Add entry to database",
description = "Save entry to database. Keys and values can be duplicated."
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Entry was added to database"),
@ApiResponse(responseCode = "500", description = "Something went wrong, entry was not added")
})
@PostMapping("/api/add")
public AddEntryResponse addEntry(@RequestBody AddEntryRequest addEntryRequest) {
database.add(new Entry(addEntryRequest.getKey(), addEntryRequest.getValue()));
return new AddEntryResponse("Success");
}
@Operation(
summary = "Delete entry from database",
description = "Deletes entry from database. If entries are duplicated, we delete only one instance." +
" Returns deleted entry"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Entry was removed from database"),
@ApiResponse(responseCode = "500", description = "Something went wrong, entry was not added")
})
@DeleteMapping("/api/remove/{key}")
public RemoveEntryResponse deleteEntry(@PathVariable String key) {
Optional<Entry> entry = database.stream().filter(e -> e.getKey().equals(key)).findFirst();
return entry.map(e -> {
database.remove(e);
return new RemoveEntryResponse(e);
}).orElse(new RemoveEntryResponse());
}
package pl.mloza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pl.mloza.database.Entry;
import pl.mloza.rest.AddEntryRequest;
import pl.mloza.rest.AddEntryResponse;
import pl.mloza.rest.ListEntryResponse;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
@RestController
public class Main {
private final List<Entry> database = new ArrayList<>();
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
public Main() {
database.add(new Entry("one", "one"));
database.add(new Entry("two", "two"));
database.add(new Entry("three", "three"));
}
@GetMapping("/api/list")
public ListEntryResponse listObjects() {
return new ListEntryResponse(database);
}
@PostMapping("/api/add")
public AddEntryResponse addEntry(@RequestBody AddEntryRequest addEntryRequest) {
database.add(new Entry(addEntryRequest.getKey(), addEntryRequest.getValue()));
return new AddEntryResponse("Success");
}
}
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.mloza</groupId>
<artifactId>swagger-ui</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
@Configuration
public class SwaggerConfiguration {
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API Demo")
.description("Demo application API")
.version("1.0.0")
.build();
}
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.enable(true)
.select()
.paths(PathSelectors.any())
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment