分类: C#/.net
2020-10-10 11:13:49
点击(此处)折叠或打开
-
new RestApplicationServer().run(new RestConfiguration {
-
StaticFileConfigurations = new List<StaticFileConfiguration>() { new StaticFileConfiguration("/e", "E:\\"), new StaticFileConfiguration("/f", "F:\\")
-
}
-
});
点击(此处)折叠或打开
-
[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";
-
}
-
}
点击(此处)折叠或打开
-
[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());
-
}
-
}
controller和service上增加Component注解后,服务启动时会进行自动扫描
点击(此处)折叠或打开
- class Program
- {
- static void Main(string[] args)
- {
- new RestApplicationServer().run();
- }
- }
点击(此处)折叠或打开
- [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注解后,服务启动时会进行自动扫描
项目地址: