Spring Boot 之HelloWorld开发案例

网友投稿 215 2023-05-21


Spring Boot 之HelloWorld开发案例

1.开发工具安装

在Eclipse上安装插件:spring Tool Suite(简称STS)

2.开发实例

1).创建项目

File > New > Spring Starter Project

项目创建完成:

2).生成的源码解读

SpringBootSimpleApplication类:

package com.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringBootSimpleApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootSimpleApplication.class, args);

}

}

pom.xml文件:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

Spring-boot-simple

0.0.1-SNAPSHOT

jar

Spring-boot-simple

<description>Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

Spring-boot-simple

0.0.1-SNAPSHOT

jar

Spring-boot-simple

<description>Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

3).自定义Controller层

创建HelloWorldController.java类:

package com.example.controller;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotsckDXdcBation.RestController;

@RestController

@RequestMapping("/hello")

public class HelloWorldController {

@RequestMapping

public String hello() {

return "Hello Spring-Boot";

}

@RequestMapping("/info")

public Map getInfo(@RequestParam String name) {

Map map = new HashMap();

map.put("name", name);

return map;

}

@RequestMapping("/list")

public List> getList() {

List> list = new ArrayList>();

Map map = null;

for (int i = 1; i <= 5; i++) {

map = new HashMap<>();

map.put("name-"+i, "spring-boot-"+i);

list.add(map);

}

return list;

}

}

然后现在可以直接运行 SpringBootSampleApplication 的main方法,和执行普通java程序一样。

然后可以看到spring-boot 内置server容器(默认为Tomcat),这一切spring-boot 都帮我们做好了。

在浏览器输入我们3个请求便可看到结果。

http://localhost:8080/hello

输出:Hello Spring-Boot

http://localhost:8080/hello/info?name=spring-boot

输出:{“name”:”spring-boot”}

http://localhost:8080/hello/list

输出:[{“name-1”:”spring-boot-1”},{“name-2”:”spring-boot-2”},{“name-3”:”spring-boot-3”},{“name-4”:”spring-boot-4”},{“name-5”:”spring-boot-5”}]

通过我们的Hello实例,相信大家一目了然,可谓spring-boot创建一个项目如此简单,完全可以在几分钟内将服务启动。

3.注解说明

1).@SpringBootApplication

很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。

该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。

2).@RestController和@RequestMapping注解

我们的Example类上使用的第一个注解是@RestController。被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。在本示例中,我们的类是一个web @Controller ,所以当处理进来的web请求时,Spring会询问它。

@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 home 方法。

@RestController 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者。

注: @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boot的特定部分)

以上所述是给大家介绍的Spring Boot 之HelloWorld开发案例,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot的filter(过滤器)简单使用实例详解
下一篇:spring boot启动加载数据原理分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~