【springboot第一个案例】快速搭建springboot

    xiaoxiao2022-07-13  163

    上面那章我们搭建了一个空的maven项目,下面我们来搭建springboot项目

    项目目录如下:

     

     

    首先修改pom文件,在pom里加入springboot依赖

    <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <groupId>com.example</groupId> <artifactId>myfirstspringbootdemotwo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>myfirstspringbootdemo Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>myfirstspringbootdemo</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>

    MyApplication.java

    package com.wangyl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @Controller public class MyApplication { // //public static void main(String[] args){ // System.out.println("ssss"); // } @RequestMapping("/") @ResponseBody String index() { return "Hello World"; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }

     

     

    StudentController.java package com.wangyl.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/student") public class StudentController { @RequestMapping("/index") @ResponseBody public String index() { return "<html>\n" + "\n" + "<body style=\"background-color:yellow\">\n" + "<h2 style=\"background-color:red\">This is a heading</h2>\n" + "<p style=\"background-color:green\">This is a paragraph.</p>\n" + "</body>\n" + "\n" + "</html>"; } @RequestMapping("/hello") @ResponseBody public String hello() { return "hello"; } }

     

     

    clean,install成功后,启动我们的

    MyApplication项目即可,然后访问http://localhost:8080/student/index

    会出现如下界面:

     

    好了,springboot的第一个项目就讲到这里。

    最新回复(0)