Spring boot使用 hello world

来源:互联网 发布:java项目部署到tomcat 编辑:程序博客网 时间:2024/06/01 21:56
方式一:
创建一个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>springboots</groupId>    <artifactId>com.springboot</artifactId>    <version>1.0-SNAPSHOT</version>    <!-- 继承Spring boot 默认值 -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.BUILD-SNAPSHOT</version>    </parent>    <!--添加典型的依赖关系到应用程序中 -->    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <!-- 打包一个可执行的jar -->    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>    <!--添加Spring存储库 -->    <!-- (如果您使用的是.RELEASE版本,则不需要此功能) -->    <repositories>        <repository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>            <snapshots><enabled>true</enabled></snapshots>        </repository>        <repository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>        </pluginRepository>        <pluginRepository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </pluginRepository>    </pluginRepositories></project>
创建ApplicationServer与Example两个类

ApplicationServer.java内容:
package myFirstProject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by Lzh on 2017/7/14. */@SpringBootApplicationpublic class ApplicationServer {    public static void main(String[] args) {        SpringApplication.run(ApplicationServer.class, args);    }} 
我们的应用程序最后部分是main方法。这只是一个标准的方法,它遵循Java对于一个应用程序入口点的约定。我们的main方法通过调用run,将业务委托给了Spring Boot的SpringApplication类。SpringApplication将引导我们的应用,启动Spring,相应地启动被自动配置的Tomcat web服务器。我们需要将 Example.class 作为参数传递给run方法来告诉SpringApplication谁是主要的Spring组件。为了暴露任何的命令行参数,args数组也会被传递过去
Example.java内容:
package myFirstProject;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by Lzh on 2017/7/14. */@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home() {        return "Hello World!";    }    @RequestMapping("/hello/{myName}")    String index(@PathVariable String myName) {        return "Hello "+myName+"!!!";    }}
我们的Example类上使用的第一个注解是 @RestController 。这被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。
对于Spring,该类扮演了一个特殊角色。在本示例中,我们的类是一个web @Controller ,所以当处理进来的web请求时,Spring会询问它。
@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 home方法。
@RestController 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者。
注: @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boot的特定部分)。具体查看Spring参考文档的MVC章节
@EnableAutoConfiguration注解 第二个类级别的注解是 @EnableAutoConfiguration 。这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置
启动ApplicationServer中的主函数。会在控制输出如下图所示的信息

然后在浏览器中访问地址
localhost:8080
localhost:8080/hello/God
页面会输出:




点击 ctrl-c 温雅地关闭应用程序。