源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Spring Boot 之HelloWorld开发案例

  • 时间:2021-11-17 11:05 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Spring Boot 之HelloWorld开发案例
[b]1.开发工具安装[/b] 在Eclipse上安装插件:spring Tool Suite(简称STS) [b]2.开发实例[/b] 1).创建项目 File > New > Spring Starter Project [img]http://files.jb51.net/file_images/article/201704/201704241145282.png[/img] [img]http://files.jb51.net/file_images/article/201704/201704241145293.png[/img] [img]http://files.jb51.net/file_images/article/201704/201704241145294.png[/img] [img]http://files.jb51.net/file_images/article/201704/201704241145295.png[/img] 项目创建完成: [img]http://files.jb51.net/file_images/article/201704/201704241145296.png[/img] 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文件:
<?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-simple</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>Spring-boot-simple</name>
 <description>Demo project for Spring Boot</description>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.2.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-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>
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.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloWorldController {
 @RequestMapping
 public String hello() {
  return "Hello Spring-Boot";
 }
 @RequestMapping("/info")
 public Map<String, String> getInfo(@RequestParam String name) {
  Map<String, String> map = new HashMap<String,String>();
  map.put("name", name);
  return map;
 }
 @RequestMapping("/list")
 public List<Map<String, String>> getList() {
  List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  Map<String, String> 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个请求便可看到结果。 [url=http://localhost:8080/hello]http://localhost:8080/hello[/url] 输出:Hello Spring-Boot [url=http://localhost:8080/hello/info?name=spring-boot]http://localhost:8080/hello/info?name=spring-boot[/url] 输出:{“name”:”spring-boot”} [url=http://localhost:8080/hello/list]http://localhost:8080/hello/list[/url] 输出:[{“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创建一个项目如此简单,完全可以在几分钟内将服务启动。 [b]3.注解说明[/b] 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开发案例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部