栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

通过简单方法实现spring boot web项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过简单方法实现spring boot web项目

搭建效果为:

直接在网页输入请求,在页面中显示一行文字:Hello,Spring Boot

与一般的wen项目不同的地方:

1、不需要配置web.xml 文件,但需要注解@SpringBootApplication 等

2、一切和spring有关的jar包都不需要版本号,springcloud会给你选择它最稳定的版本

3、它会定位public static void main()方法来标记为可运行类,必须在主路径下

4、启动方式:

  a.右键运行main方法

  b.由于我们使用了 spring-boot-starter-parent POM,所以可以使用 mvn spring-boot:run来启动项目(根路径)

  c.先使用Maven来package(打包),然后java -jar*-0.0.1-SNAPSHOT.jar。

搭建

创建一个新的maven项目,目录结构如下:

pom.xml文件



  4.0.0

  cn.jiashubing
  spring-boot-web
  1.0-SNAPSHOT

  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
  

  
    
      
 org.springframework.boot
 spring-boot-maven-plugin
      
    
  

HomeController.java文件

package cn.jiashubing.controller;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@EnableAutoConfiguration
public class HomeController {

  @RequestMapping(value = "/home", method = RequestMethod.GET)
  @ResponseBody
  public String home() {
    return "Hello,Spring Boot";
  }
}

JiashubingApplication.java文件

package cn.jiashubing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

在浏览器中输入 http://localhost:8080/home

最终效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号