spring-boot 的hello world

环境搭建有问题的请单独沟通
1. jdk环境
本文介绍的基于jdk1.8
2. maven环境
maven版本3.3.9
3. 创建一个maven项目
在eclipse中创建一个maven项目
4. 添加依赖
在pom.xml 里边添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
  1. 编写springboot的启动器代码
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

@SpringBootApplication
这个注解是使用的springboot的默认配置
介绍几个常用的注解
@EnableAutoConfiguration
开启自动注入
@ComponentScan
指定扫描包
@ImportResource({ “classpath:applicationContext.xml” })
指定引入配置资源文件
6. 创建一个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 生成数据类型

  1. 模板示例
    添加模板依赖
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
    @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>
  1. rest风格代码示例
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") int id) {
    return String.format("delete %d", id);
}
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值