如何实现一个spring boot web项目?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.jiashubing</groupId>
<artifactId>spring-boot-web</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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;
/**
* @author jiashubing
* @since 2018/5/30
*/
@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;
/**
* @author jiashubing
* @since 2018/5/29
*/
@SpringBootApplication
public class JiashubingApplication {
public static void main(String[] args) {
SpringApplication.run(JiashubingApplication.class, args);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。