114
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
builder.Services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings();
options.SwaggerDoc(“v1”, new OpenApiInfo { Title =
“eShopOnContainers
-
Catalog HTTP API”,
Version = “v1”, Description = “The Catalog Microservice HTTP API.
This is a Data
-Driven/CRUD
microservice sample” }); });
// Other startup code…
app.UseSwagger() .UseSwaggerUI(c => { c.SwaggerEndpoint(“/swagger/v1/swagger.json”, “My API
V1”); }); ``` :::
Once this is done, you can start your application and browse the following Swagger JSON and UI
endpoints using URLs like these:
http://
/swagger/v1/swagger.json
http:///swagger/
You previously saw the generated UI created by Swashbuckle for a URL like
http://url>/swagger
. In Figure 6-9, you can also see how you can test any API method.
115
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Figure 6-9. Swashbuckle UI testing the Catalog/Items API method
The Swagger UI API detail shows a sample of the response and can be used to execute the real API,
which is great for developer discovery. Figure 6-10 shows the Swagger JSON
metadata generated
from the eShopOnContainers microservice (which is what the tools use underneath)
when you request
http://
/swagger/v1/swagger.json
using
Postman
.
116
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Figure 6-10. Swagger JSON metadata
It is that simple. And because it
is automatically generated, the Swagger metadata will grow when you
add more functionality to your API.
Dostları ilə paylaş: