Repository navigation
WEB
bdq edited this page Feb 6, 2020
·
9 revisions
开发 festival 项目建议使用 maven,而不是使用传统手工的方式去管理 jar 包和构建项目。
创建maven项目,导入festival-web必要依赖,在pom.xml中添加如下依赖:
<dependency>
<artifactId>festival-web</artifactId>
<groupId>com.github.bdqfork</groupId>
<version>{lasted.version}</version>
</dependency>项目需要jdk1.8以上版本支持,可以在maven中添加插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>新建一个HelloController类加上@Route注解来使之成为路由,并创建一个hello方法,通过@GetMapping将请求映射到该方法。
@Singleton
@Route
public class HelloController {
//使用@GetMapping注解指明对应的url和请求方法
@GetMapping("/hello")
public String hello() {
return "hello festival";
}
}新建项目启动类,添加main方法,实例化WebApplicationContext对象并执行其start()方法,注意其构造方法WebApplicationContext(String... scanPaths) 中的scanPaths对应的包应当包含HelloController类以及其他所有需要的Beans:
public class DemoApplicationRunner {
public static void main(String[] args) throws Exception {
WebApplicationContext webApplicationContext = new WebApplicationContext("xyz.lennon.example");
webApplicationContext.start();
}
}启动DemoApplicationRunner类,打开浏览器输入localhost:8080/hello,即可得到返回值.