spring-boot 的hello world
环境搭建有问题的请单独沟通
- jdk环境
本文介绍的基于jdk1.8 - maven环境
maven版本3.3.9 - 创建一个maven项目
在eclipse中创建一个maven项目 添加依赖
在pom.xml 里边添加依赖
org.springframework.boot
spring-boot-starter-web
1.4.4.RELEASE
编写springboot的启动器代码
@SpringBootApplication
public class Application {public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
这个注解是使用的springboot的默认配置
介绍几个常用的注解
@EnableAutoConfiguration
开启自动注入
@ComponentScan
指定扫描包
@ImportResource({ “classpath:applicationContext.xml” })
指定引入配置资源文件
application.properties配置文件
#访问端口
server.port=7001
#监控端口
management.port=7002
监控依赖jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
创建一个controller
@RestController
public class DemoController {@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String hello(String message) {
return message;
}
}
基本配置和spring mvc相同
@RequestMapping
value 请求地址
method 请求支持方法
produces 生成数据类型
模板示例
添加模板依赖
org.springframework.boot
spring-boot-starter-thymeleaf
1.4.4.RELEASE
@RequestMapping(“/“)
public ModelAndView index(HttpServletRequest request) throws Exception {
Map<String, Object> model = Maps.newConcurrentMap();
model.put("name", "name");
return new ModelAndView("index", model);
}
在resources下创建templates/index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="${name}"></p>
</body>
</html>
rest风格代码示例
@RequestMapping(“/delete/{id}”)
public String delete(@PathVariable(“id”) int id) {return String.format("delete %d", id);
}
还没有评论,来说两句吧...