Series Articles
- Introduction to. Net Core Distributed micro-service framework-Jimu
- . Net Core Distributed microservices Framework-Jimu Add Swagger support
First, preface
Recently available to optimize the Jimu (a distributed micro-service framework based on. Net Core), considering the current development organization to the front and back of the separation of development, the front and back of each side, good API documentation can reduce the time cost of communication, so give priority to Jimu to add support for API document generation. The most famous and awesome API document generation framework on the market is non-swagger. It can be used to generate, describe, and invoke a visual Web service. It took more than two days to integrate it into Jimu's apigateway.
API List
API details
Second, the mode of use 1. Environment
- Net Core 2.0
- Apigateway based on the JIMU framework
2. Adding references
Install-Package Jimu.Client.ApiGateway.SwaggerIntegration
3. Startup code
Add Usejimuswagger () in Startup
public void ConfigureServices(IServiceCollection services) { services.UseJimuSwagger(); // 添加 Swagger 支持 } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseJimuSwagger(); // 添加 Swagger 支持
4. Defining interfaces (services/methods)
The interface definition can add three annotations that will eventually appear in the swagger generated document.
A. jimuservice annotations are definitions and descriptions of interface metadata, such as creator, time, description, Access role restrictions, and so on.
b. The jimufieldcomment Callout is a comment on the formal parameter.
c. jimureturncomment Annotations are comments on the return type of the interface.
[JimuService(EnableAuthorization = true, CreatedBy = "grissom", CreatedDate = "2018-07-17", Comment = "根据新闻 id 获取新闻内容")] [JimuFieldComment("id", "新闻id")] [JimuReturnComment("一篇新闻内容")] News GetNews(string id);
5. Defining Objects (DTOs)
If the interface parameter or return type is a user-defined class, the corresponding property can also add annotation callout jimufieldcomment, which will eventually appear in the swagger generated document.
public class News { [JimuFieldComment("新闻id")] public string Id { get; set; } [JimuFieldComment("新闻标题")] public string Title { get; set; } [JimuFieldComment("作者")] public string Director { get; set; } [JimuFieldComment("发布时间")] public string PostTime { get; set; } [JimuFieldComment("新闻内容")] public string Content { get; set; } }
Three, illustrations
Iv. Summary
Support for open source is very tiring and time-consuming work, but the open source process, I also learned a lot of knowledge, hope can continue to persist.
Five, the source code
Https://github.com/grissomlau/jimu
Https://github.com/grissomlau/jimu.demo
. Net Core Distributed microservices Framework-Jimu Add Swagger support