spring-boot 的hello world

刺骨的言语ヽ痛彻心扉 2022-06-18 02:21 307阅读 0赞

环境搭建有问题的请单独沟通

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


    org.springframework.boot
    spring-boot-starter-web
    1.4.4.RELEASE
  5. 编写springboot的启动器代码

    @SpringBootApplication
    public class Application {

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

    }

@SpringBootApplication
这个注解是使用的springboot的默认配置
介绍几个常用的注解
@EnableAutoConfiguration
开启自动注入
@ComponentScan
指定扫描包
@ImportResource({ “classpath:applicationContext.xml” })
指定引入配置资源文件

application.properties配置文件

  1. #访问端口
  2. server.port=7001
  3. #监控端口
  4. management.port=7002

监控依赖jar

  1.  <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4.   </dependency>
  1. 创建一个controller

    @RestController
    public class DemoController {

    1. @RequestMapping(value = "/hello", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    2. public String hello(String message) {
    3. return message;
    4. }

    }

基本配置和spring mvc相同
@RequestMapping
value 请求地址
method 请求支持方法
produces 生成数据类型

  1. 模板示例
    添加模板依赖


    org.springframework.boot
    spring-boot-starter-thymeleaf
    1.4.4.RELEASE

    @RequestMapping(“/“)

    1. public ModelAndView index(HttpServletRequest request) throws Exception {
    2. Map<String, Object> model = Maps.newConcurrentMap();
    3. model.put("name", "name");
    4. return new ModelAndView("index", model);
    5. }

在resources下创建templates/index.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <body>
  4. <p th:text="${name}"></p>
  5. </body>
  6. </html>
  1. rest风格代码示例

    @RequestMapping(“/delete/{id}”)
    public String delete(@PathVariable(“id”) int id) {

    1. return String.format("delete %d", id);

    }

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Hello World

    Hello World 一、简述          简单的Hello World程序。(时间久了就会忘,趁着还有印象先记下)     1、C语言:  控制台程序、有窗体