Spring Boot学习笔记——一个简单的Web应用程序

来源:互联网 发布:剑三萝莉捏脸数据2017 编辑:程序博客网 时间:2024/06/09 22:21

一个简单的Web应用程序

创建一个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>com.example</groupId>    <artifactId>spring-boot-demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>spring-boot-demo1</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>        <relativePath /> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

创建一个Controller

package com.demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController// @RestController表示使用Spring MVC来处请网络请求// @RestController 是 @Controller 与 @ResponseBody 的结合,表示返回数据,而不是返回一个Viewpublic class HelloController {    // @RequestMapping("/")表示映射路径 /    // 也就是访问 "localhost:8080/" 时会执行index()方法    @RequestMapping("/")    public String index() {        return "Hello, world";    }}

创建 Application 类,Application是项目启动的地方,而且需要放置在最顶层的包(要包括所有用到的包),比如你有 “com.demo”, “com.demo.application”两个包,你将 Application 类放到 com.demo.application中会无法扫描 “com.demo”下的类。

package com.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

@SpringBootApplication 注解包括以下注解的功能:

  • @Configuration 将类标记为应用程序上下文的bean定义的来源。
  • @EnableAutoConfiguration 告诉Spring Boot根据类路径设置,其他bean和各种属性设置开始添加bean。
  • @ComponentScan:告诉Spring在 com.demo 包中寻找其他组件,配置和服务,让它找到控制器。
  • @EnableWebMvc:通常,您将为Spring MVC应用程序添加@EnableWebMvc,但是当Spring类在类路径中看到spring-webmvc时,Spring Boot会自动添加它。 这将应用程序标记为Web应用程序,并激活诸如设置DispatcherServlet等关键行为。

main()方法使用SpringApplication.run()方法来启动一个应用。

仔细看一下项目,你会发现连web.xml都没有,该程序完全由Java来编写。

运行程序

在项目上右键 Run As -> Maven build... 在Goals行输入 spring-boot:run 点击 Run 即可

spring-boot:run

不报错的话,表明运行成功。

成功启动

测试,打开浏览器,输入 “localhost:8080”,得到 “Hello, World”。

这里写图片描述

简单的一个Web项目就搭建完成了~

阅读全文
0 0
原创粉丝点击