SpringBoot 入门小程序

    xiaoxiao2026-01-01  9

    SpringBoot 入门小程序下载

    SpringBoot 入门小程序GitHub下载

    老规矩,先看项目结构。

    有必要参考前面的博客。maven构建web工程

    我们构建好maven工程之后,用pom导入jar包。

    pom.xml

    <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.hust.springboot</groupId> <artifactId>SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <!-- Spring Boot web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

    然后我们需要一个 controller

    @RestController告诉Spring以字符串的形式渲染结果,并直接返回给调用者。@EnableAutoConfiguration 。这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。

    HelloController.java

    package com.hust.springboot.controller; 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; import com.hust.springboot.dao.People; @EnableAutoConfiguration @RestController public class HelloController { @RequestMapping("/") public String sayHello() { return "Hello,World!"; } @RequestMapping("/{id}") public People getPeople(@PathVariable Integer id) { People people = new People(); people.setId(id); people.setAge("20"); people.setName("yexx"); return people; } }

    我们的main方法通过调用run,将业务委托给了spring Boot的SpringApplication类。SpringApplication将引导我们的应用,启动Spring,相应地启动被自动配置的Tomcat web服务器。 我们需要将 HelloController.class 作为参数传递给run方法来告诉SpringApplication谁是主要的Spring组件。为了暴露任何的命令行参数,args数组也会被传递过去。

    Application.java

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

    然后我们run起来看到如下的特效说明成功了。

    然后去浏览器访问就搞定啦。

    SpringBootDemo生成官网

    http://start.spring.io/

    相关资源:微信小程序后端Springboot
    最新回复(0)