网站首页 > 技术文章 正文
一、说明
- 本文使用IDEA创建一个基于SpringBoot的restful服务;
- 需要安装jdk1.8 gradle
- 本文学习内容来自SpringBoot官网。
二、步骤
1. IDEA新建项目
2. 写build.gradle内容
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group 'com.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
等待窗口自动下载完依赖包。
现在目录结构:
3. 创建服务
创建类 src/main/java/hello/Greeting.java
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;
}
}
Springboot使用 Jackson JSON 库来封装JSON。
创建Controller src/main/java/hello/GreetingController.java
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));
}
}
@RequestMapping 注解
将http请求从/greeting映射到greeting()方法 ,没有指明GET PUT POST,它会接受所有HTTP请求。
如果要指定,可以写:@RequestMapping(method=GET)
@RequestParam 绑定请求参数
上面的示例创建并返回一个新的Greeting对象,包含id和Content属性。Content从counter赋值,name使用了template模板。
@RestController
标记类是一个Controller,返回domain对象而不是view。 相当于@Controller + @ResponseBody。
返回JSON
Spring的MappingJackson2HttpMessageConverter自动将Greeting 转为JSON格式。
创建启动函数 src/main/java/hello/Application.java
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 会完成以下注解的作用:
- @Configuration 标记这个类作为Spring上下文的bean定义
- @EnableAutoConfiguration 告诉SpringBoot基于classpath的一些配置文件自动配置bean。比如,spring-webvc如果在classpath里,这个注解会让应用程序作为一个web应用,这样就会自动设置 如DispatcherServlet的属性。
- @ComponentScan:告诉Spring在hello包下扫描组件、配置、service、controller等。
这个示例不需要使用web.xml。
三、编译运行
1. 在命令行输入: ./gradlew bootrun
在浏览器输入:http://localhost:8080/greeting
输出:
{"id":1,"content":"Hello, World!"}
2. 也可以使用Gradle菜单运行:
3. 运行编译后的jar
选择Gradle菜单的assemble
在build目录下生成jar,右击并选择 Open In Terminal,输入命令:
jar -jar myrestful-1.0-SNAPSHOT.jar
猜你喜欢
- 2024-11-24 使用Knative部署基于Spring Native的微服务
- 2024-11-24 阿里p7大佬首次分享Spring Cloud学习笔记,带你从0搭建微服务
- 2024-11-24 ElasticSearch进阶篇之搞定在SpringBoot项目中的实战应用
- 2024-11-24 SpringCloud微服务架构实战:类目管理微服务开发
- 2024-11-24 SpringBoot+SpringCloud题目整理
- 2024-11-24 《github精选系列》——SpringBoot 全家桶
- 2024-11-24 SpringCloud系列:多模块聚合工程基本环境搭建「1」
- 2024-11-24 Spring Cloud Consul快速入门Demo
- 2024-11-24 Spring Cloud Contract快速入门Demo
- 2024-11-24 15年大牛用140多个实战案例深入讲解Java微服务架构文档
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 506℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 486℃MySQL service启动脚本浅析(r12笔记第59天)
- 466℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 464℃启用MySQL查询缓存(mysql8.0查询缓存)
- 444℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 423℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 419℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)