【springboot】搭建springboot项目
简单搭建
1 添加依赖spring-boot-starter-web
dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.5.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' }
2 建立启动类 ApiApplicationContext(这一步项目可以启动)
@SpringBootApplication public class ApiApplicationContext { public static void main(String[] args) { SpringApplication.run(ApiApplicationContext.class, args); } }
3 提供http接口
@RestController//用友@Controller 、 @ResponseBody注解的功能 public class HelloController { @RequestMapping("/say-hello") public String sayHello(){ return "hello"; } }
4、渲染页面(freemarker)
添加依赖
dependencies { //springboot启动相关包 compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.5.RELEASE' //freemarker compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker', version: '2.3.5.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' }
添加路由
@Controller @RequestMapping("/view") public class PageController { @RequestMapping("/home-page") public String homePage(Model model) { model.addAttribute("name","mojing"); return "home_page"; } }
添加ftl页面 home_page.ftl
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> hello: ${name} </body> </html>
freemarker配置(application.yml)
spring: freemarker: template-loader-path: classpath:/templates/ suffix: .ftl cache: false request-context-attribute: request content-type: text/html