- 静态页面处理
- GET/POST/PUT/DELETE请求
- 支持返回JSON
- 支持路由方法
- 支持自定义过滤器
- 服务器返回数据支持gzip压缩
- 支持Component变量自动注入
- 支持Component自动扫描
- GET/POST支持查询参数,POST支持body数据
- 注解支持
- Component
- WebFilter
- RequestMapping
- Autowired
- RequestBody
- RequestParam
new RestApplicationServer().run(new RestConfiguration {
StaticFileConfigurations = new List<StaticFileConfiguration>() {
new StaticFileConfiguration("/e", "E:\\"),
new StaticFileConfiguration("/f", "F:\\")
}
})
// 将 http://xxxxx.com/e/xxxxx 映射到本地磁盘文件 E:\\xxxxx
// 将 http://xxxxx.com/f/xxxxx 映射到本地磁盘文件 F:\\xxxxx
1.添加Controller
[Component("PersonController")]
public class PersonController
{
[Autowired]
public PersonService personService
private ILogger logger = LoggerFactory.GetLogger()
[RequestMapping("GET","/api/person/list")]
public List<Person>GetPersonList()
{
return personService.GetPersonList()
}
[RequestMapping("GET", "/api/person")]
public Person GetPerson([RequestParam("id")]int id)
{
logger.Debug("id:"+id)
return personService.GetPerson((int)id)
}
[RequestMapping("POST", "/api/person")]
public string Create([RequestBody] Person person)
{
logger.Info("person:" + person.ToString())
return "ok"
}
}
2.添加Service
[Component("PersonService")]
public class PersonService
{
private ILogger logger = new ConsoleLogger()
public List<Person>GetPersonList() {
return TestData.PersonList
}
public Person GetPerson(int id)
{
return TestData.PersonList.Find(x =>x.id == id)
}
public void Create(Person person)
{
logger.Debug(person.ToString())
}
}
3.启动服务
controller和service上增加Component注解后,服务启动时会进行自动扫描
class Program
{
static void Main(string[] args)
{
new RestApplicationServer().run()
}
}
1. 添加一个计算接口处理耗时的filter
[WebFilter(1, "/")]
public class StopWacthFilter : IFilter
{
public void Filter(HttpRequest request,ref HttpResponse response, ProcessChain chain, int nextIndex)
{
Stopwatch stopwatch = new Stopwatch()
stopwatch.Start()
chain.doFilter(request,ref response, nextIndex)
stopwatch.Stop()
Console.WriteLine(request.Method + " "+request.Path+ ", 耗时:"+(stopwatch.ElapsedMilliseconds).ToString()+"ms")
}
}
自定义filter上增加WebFilter注解后,服务启动时会进行自动扫描
项目地址: https://github.com/13401095975/RestServer
2.7 configHandler(..)此方法用来配置JFinal的Handler,如下代码配置了名为ResourceHandler的处理器,Handler可以接管所有web请求,并对应用拥有完全的控制权,可以很方便地实现更高层的功能性扩展。
public void configHandler(Handlers me) {me.add(new ResourceHandler())}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)