-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Swagger API
This page has moved to docs.servicestack.net/swagger-api
Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services. ServiceStack implements the Swagger 1.2 Spec back-end and embeds the Swagger UI front-end in a separate plugin which is available under Swagger NuGet package:
PM> Install-Package ServiceStack.Api.Swagger
You can enable Swagger by registering the SwaggerFeature
plugin in AppHost with:
public override void Configure(Container container)
{
...
Plugins.Add(new SwaggerFeature());
// uncomment CORS feature if it's has to be available from external sites
//Plugins.Add(new CorsFeature());
...
}
Then you will be able to view the Swagger UI from /swagger-ui/
. A link to Swagger UI will also be available from your /metadata
Metadata Page.
If you're Hosting ServiceStack with MVC then you'll need to tell MVC to ignore the path where ServiceStack is hosted, e.g:
routes.IgnoreRoute("api/{*pathInfo}");
For MVC4 projects, you'll also need to disable WebAPI:
//WebApiConfig.Register(GlobalConfiguration.Configuration);
Each route could have a separate summary and description. You can set it with Route
attribute:
[Route("/hello", Summary = @"Default hello service.",
Notes = "Longer description for hello service.")]
You can set specific description for each HTTP method like shown below:
[Route("/hello/{Name}", "GET", Summary="Says 'Hello' to provided Name",
Notes = "Longer description of the GET method which says 'Hello'")]
[Route("/hello/{Name}", "POST", Summary="Says 'Hello' to provided Name",
Notes = "Longer description of the POST method which says 'Hello'")]
You can further document your services in the Swagger UI with the new [Api]
and [ApiMember]
annotation attributes, e,g: Here's an example of a fully documented service:
[Api("Service Description")]
[ApiResponse(HttpStatusCode.BadRequest, "Your request was not understood")]
[ApiResponse(HttpStatusCode.InternalServerError, "Oops, something broke")]
[Route("/swagger/{Name}", "GET", Summary = "GET Summary", Notes = "Notes")]
[Route("/swagger/{Name}", "POST", Summary = "POST Summary", Notes="Notes")]
public class MyRequestDto
{
[ApiMember(Name="Name", Description = "Name Description",
ParameterType = "path", DataType = "string", IsRequired = true)]
[ApiAllowableValues("Name", typeof(Color))] //Enum
public string Name { get; set; }
}
You can Exclude properties from being listed in Swagger with:
[IgnoreDataMember]
Exclude properties from being listed in Swagger Schema Body with:
[ApiMember(ExcludeInSchema=true)]
Or exclude entire Services from showing up in Swagger or any other Metadata Services (i.e. Metadata Pages, Postman, NativeTypes, etc) by annotating Request DTO's with:
[Exclude(Feature.Metadata)]
public class MyRequestDto { ... }
The docs on the Virtual File System shows how to override embedded resources:
ServiceStack's Virtual File System supports multiple file source locations where you can override Swagger's embedded files by including your own custom files in the same location as the existing embedded files. This lets you replace built-in ServiceStack embedded resources with your own by simply copying the /swagger-ui or /swagger-ui-bootstrap files you want to customize and placing them in your Website Directory at:
/swagger-ui
/css
/images
/lib
index.html
/swagger-ui-bootstrap
index.html
swagger-like-template.html
Users can call protected Services using the Username and Password fields in Swagger UI. Swagger sends these credentials with every API request using HTTP Basic Auth, which can be enabled in your AppHost with:
Plugins.Add(new AuthFeature(...,
new IAuthProvider[] {
new BasicAuthProvider(), //Allow Sign-ins with HTTP Basic Auth
}));
Alternatively users can login outside of Swagger, to access protected Services in Swagger UI.
ServiceStack.UseCases project contains example SwaggerHelloWorld. It demonstrates how to use and integrate ServiceStack.Api.Swagger. Take a look at README.txt for more details.
- Why ServiceStack?
- Important role of DTOs
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
-
Getting Started
-
Designing APIs
-
Reference
-
Clients
-
Formats
-
View Engines 4. Razor & Markdown Razor
-
Hosts
-
Security
-
Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in profiling
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Dump Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- ServiceStack Integration
- Embedded Native Desktop Apps
- Auto Batched Requests
- Versioning
- Multitenancy
-
Caching
-
HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients
-
Auto Query
-
AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB
-
Server Events
-
Service Gateway
-
Encrypted Messaging
-
Plugins
-
Tests
-
ServiceStackVS
-
Other Languages
-
Amazon Web Services
-
Deployment
-
Install 3rd Party Products
-
Use Cases
-
Performance
-
Other Products
-
Future