Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613381
  • 博文数量: 127
  • 博客积分: 6136
  • 博客等级: 准将
  • 技术积分: 1461
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 00:32

分类: C#/.net

2020-10-10 11:13:49

为了能够在桌面端软件中简单方便的对外提供RestApi接口,参考Java SpringMVC框架使用C#语言编写了一个简易RestApi服务器框架,目前支持:
  • 静态页面处理
  • GET/POST/PUT/DELETE请求
  • 支持返回JSON
  • 支持路由方法
  • 支持自定义过滤器
  • 服务器返回数据支持gzip压缩
  • 支持Component变量自动注入
  • 支持Component自动扫描
  • 支持Filter过滤器
  • GET/POST支持查询参数,POST支持body数据(JSON格式)
  • 注解支持
    • Component
    • WebFilter
    • RequestMapping
    • Autowired
    • RequestBody
    • RequestParam

示例一 静态页面映射

 
			

点击(此处)折叠或打开

  1. new RestApplicationServer().run(new RestConfiguration {
  2.     StaticFileConfigurations = new List<StaticFileConfiguration>() { new StaticFileConfiguration("/e", "E:\\"), new StaticFileConfiguration("/f", "F:\\")
  3.   }
  4.  });

示例二 自定义路由

1.添加Controller

						

点击(此处)折叠或打开

  1. [Component("PersonController")]
  2. public class PersonController {
  3.     [Autowired]
  4.     public PersonService personService;
  5.     private ILogger logger = LoggerFactory.GetLogger();
  6.        
  7.     [RequestMapping("GET","/api/person/list")]
  8.     public List<Person> GetPersonList()
  9.     {
  10.         return personService.GetPersonList();
  11.     }
  12.     [RequestMapping("GET", "/api/person")]
  13.     public Person GetPerson([RequestParam("id")]int id)
  14.     {
  15.         logger.Debug("id:"+id); return personService.GetPerson((int)id);
  16.     }
  17.     [RequestMapping("POST", "/api/person")]
  18.     public string Create([RequestBody] Person person)
  19.     {
  20.         logger.Info("person:" + person.ToString());
  21.         return "ok";
  22.     }
  23. }

2.添加Service

								

点击(此处)折叠或打开

  1. [Component("PersonService")]
  2. public class PersonService
  3. {
  4.     private ILogger logger = new ConsoleLogger();
  5.     public List<Person> GetPersonList() {
  6.         return TestData.PersonList;
  7.     }
  8.     public Person GetPerson(int id)
  9.     {
  10.         return TestData.PersonList.Find(x => x.id == id);
  11.     }
  12.     public void Create(Person person)
  13.     {
  14.         logger.Debug(person.ToString());
  15.     }
  16. }

3.启动服务

controller和service上增加Component注解后,服务启动时会进行自动扫描

											

点击(此处)折叠或打开

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         new RestApplicationServer().run();
  6.     }
  7. }

示例三 增加Filter

1. 添加一个计算接口处理耗时的filter

													

点击(此处)折叠或打开

  1. [WebFilter(1, "/")]
  2. public class StopWacthFilter : IFilter
  3. {
  4.     public void Filter(HttpRequest request,ref HttpResponse response, ProcessChain chain, int nextIndex)
  5.     {
  6.         Stopwatch stopwatch = new Stopwatch();
  7.         stopwatch.Start();
  8.         chain.doFilter(request,ref response, nextIndex);
  9.         stopwatch.Stop();
  10.         Console.WriteLine(request.Method + " "+request.Path+ ", 耗时:"+(stopwatch.ElapsedMilliseconds).ToString()+"ms");
  11.      }
  12. }

自定义filter上增加WebFilter注解后,服务启动时会进行自动扫描

项目地址:


阅读(3076) | 评论(0) | 转发(0) |
2

上一篇:三菱QD77缓冲存储器一览表

下一篇:没有了

给主人留下些什么吧!~~