IT wheel series (iii) -- how to add comments to the return type -- Use of Swagger (II), wheel swagger

Source: Internet
Author: User

IT wheel series (iii) -- how to add comments to the return type -- Use of Swagger (II), wheel swagger

Preface

Generally, when APIs are provided externally, a uniform return type is applied. For example, all interfaces return a uniform HttpResponseMessage. In this way, when we add a // annotation to the method, we cannot clearly know what fields are returned and what their data types are. As shown in the generated document:

,

, We can see that the document only describes the input parameters, but it is unclear about the returned type and the fields. This is unfriendly to calling third-party calls.

In actual projects, it was discovered by accident that Swashbuckle provided a return type attribute, which effectively solved this problem.

As shown in the Code:

1 /// </summary> 2 public class UserController: baseController 3 {4 /// <summary> 5 // obtain user information 6 /// </summary> 7 /// <param name = "model"> obtain user information model </param> 8 // <returns> </returns> 9 [HttpPost] 10 // Add method modifier attribute return type description 11 [SwaggerResponse (HttpStatusCode. OK, Type = typeof (ResultInfo <UserInfo>)] 12 public HttpResponseMessage GetUserInfo ([FromBody] GetUserInfoModel model) 13 {14 ResultInfo <UserInfo> result = new ResultInfo <UserInfo> (); 15 try16 {17 UserInfo user = new UserInfo (); 18 user. name = "Peter"; 19 user. phone = model. phone; 20 user. email = "itwheels@163.com"; 21 result. data = user; 22} 23 catch (Exception ex) 24 {25 result. status = "FAIL"; 26 result. msg = ex. message; 27} 28 29 return toJson (result); 30} 31}
View Code

When we add the SwaggerResponse attribute to the method, we can see the return type pull in the generated document.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.