原文 : https://spring.io/guides/gs/rest-service/

类型:官网入门指南

要求:JDK1.8 ^ ,Maven 3.2 ^


最终效果

在地址栏输入 http://localhost:8080/greeting ,返回一串 JSON



如果带上 name 参数,则返回带参数值的 JSON


pom文件编写

<?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>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <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>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency> </dependencies> <properties>
<java.version>1.8</java.version>
</properties> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

其中最下面的插件 spring-boot-maven-plugin ,它获取类路径上的所有 jar 包,然后构建一个可运行的 jar 包,便于执行;

然后它会定位 public static void main() ,无论按一个类,只要包含这个方法,就会被定位到,视为可执行类;

该插件还内置一个依赖项解析器,解析 Spring Boot 的各种依赖 ;

说了这么多,其实就是一句话:该插件可以将项目打包成可运行的 jar 包 ;


编写资源类

这里创建一个 Greeting 类:

package hello;

public class Greeting {

    private final long id;
private final String content; public Greeting(long id, String content) {
this.id = id;
this.content = content;
} public long getId() {
return id;
} public String getContent() {
return content;
}
}

就跟以前学的 javabean 一样;


编写控制器

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GreetingController { private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}

首先看下类上的注解 @RestController ,区别于以前的 @Controller ,它可以视为 @Controller 、 @ResponseBody 两个注解加在一起收,标识方法返回 json;

还有一个地方需要注意,那就是处理器的方法的返回值,直接返回实例对象;

SpringBoot 将自动的将处理器方法的返回值转为 JSON,不需要我们自己手动转换,但是需要我们导入 Jackson 2 的包,然后SpringMappingJackson2HttpMessageConverter 自动的将方法的返回值实例转为 JSON 对象 ;


运行项目

编写一个类:

package hello;

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

使用 @SpringBootApplication 注解标识下,然后就可以直接运行项目了,正如你猜的那样,不需要 Tomcat

(一)SpringBoot Demo之 Hello World的更多相关文章

  1. spring-boot - demo

    当我发现把最初的一个demo整的面目全非的时候,突然想要找一个简单的demo做测试,发现与其在原来的上面该,还不如新建一个demo. 官方入门:http://projects.spring.io/sp ...

  2. 初识gradle, idea+springboot Demo

    写在前面; 使用maven管理写过几个springboot的系统, 此篇博客纯属记录整理学习的过程. 另外, 源码分享地址在最后. Java: 1.8.0_281 tomcat: 1.8 IDE: I ...

  3. SpringBoot Demo

    Spring Boot,微框架,确实不错,很方便. 相关网站链接: http://www.tuicool.com/articles/veUjQba https://www.gitbook.com/bo ...

  4. springboot demo(二)web开发demo

    如入门般建立项目,引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</g ...

  5. springboot demo(一)快速开始

    快速入门 maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.26以及一些工程基本信息,点击" ...

  6. Spring Boot整合Dubbo框架demo

    Dubbo框架原理见之前的博文:http://www.cnblogs.com/umgsai/p/5836925.html 首先启动zookeeper Server端 Pom配置如下 <?xml ...

  7. springboot 学习笔记(一)

    引子 最近在搞一个项目,走在科技前沿的师兄, 摒弃了公司老套的框架模式, 采用了springboot搭建新应用.看到如此简洁的代码 , 深受诱惑.趁周末闲余之时, 背晒阳光, 学起了springboo ...

  8. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  9. SpringBoot核心注解应用

    1.今日大纲 了解Spring的发展 掌握Spring的java配置方式 学习Spring Boot 使用Spring Boot来改造购物车系统 2.Spring的发展 Spring1.x 时代 在S ...

随机推荐

  1. Latex 数字加粗后变宽 Latex bold without increasing the length of the text

    Add the following code at the beginning of the article \newsavebox\CBox \def\textBF#1{\sbox\CBox{#1} ...

  2. laravel文件存储、删除、移动等操作

    laravel文件存储.删除.移动等操作 一.总结 一句话总结: 启示:可以在操作遇到问题的时候,找文档找实例好好实验一下,也就是学习巩固一下,不必一定要死纠排错 1.laravel文件删除注意? 1 ...

  3. Linux下通过nmap扫描局域网内设备,获取ip地址和mac地址

    安装nmap sudo apt-get install nmap 扫描  sudo nmap -sP -PI -PT

  4. iTunes Connect上传APP屏幕快照图片失败 - 您必须上传有效的屏幕快照。

    您必须上传有效的屏幕快照. 原因很简单:这个屏幕快照 要用 iPhone截屏才可以,你自已随便在电脑上截个图肯定不行 //--------------------------------------- ...

  5. UE4虚幻引擎独立游戏制作教程 UE4编程教学 虚幻引擎4

    非常好的一套UE4入门教学课程,语言诙谐幽默,并且是中文语音中文语音中文语音 赠送[精通Unreal引擎技术——关卡设计艺术]PDF版 目录 FLV格式,大小5G,中文语音 扫码时备注或说明中留下邮箱 ...

  6. 通过字节码分析this关键字以及异常表的作用

    1.创建MyTest3类 public class MyTest3 { public void test(){ try { InputStream is = new FileInputStream(& ...

  7. fingerprint for the ECDSA key

    验证  fingerprint for the ECDSA key ssh-keygen -t  ecdsa  -f ssh_host_ecdsa_key 在B上ssh A ,得到A的fingerpr ...

  8. B站在微服务治理中的探索与实践

    https://mp.weixin.qq.com/s/_iFe8DO1e-QcYG-CJDTHpg

  9. Activity: launchMode 和 Intent.FLAG_ACTIVITY_CLEAR_TOP

    Activity 的 launchMode: 1. standard: 标准模式 这种启动模式为标准模式,也是默认模式.每当我们启动一个Activity,系统就会相应的创建一个实例,不管这个实例是否已 ...

  10. 使用Dapper.Contrib

    public T Query(string sql, object param) { using (IDbConnection dbConnection = Connection) { if (dbC ...