版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014248473/article/details/88020794
SpringBoot发布一个简单Restful服务
一、环境介绍
【IDE】IDEA14
【JDK】1.8
【OS】windows7-64位
二、创建项目
1.打开 IDEA
2.点击 Create New Project
3.左侧菜单选择Maven,Project SDK选择1.8,不选择archetype,直接Next
4.填写groupId、artifactId和version,如下
###############################
# GroupId:com.yale #
# ArtifactId:springboot-rest #
# Version:1.0-SNAPSHOT #
###############################
5.填写项目信息,如下
------------------------------------------------------------------------
Project name:springboot-rest
Project location:D:\idea_workspace\springboot_test\springboot-rest
Module name:springboot-rest
Content root:D:\idea_workspace\springboot_test\springboot-rest
Module file location:D:\idea_workspace\springboot_test\springboot-rest
Project format:.idea(directory based)
------------------------------------------------------------------------
6.点击finish完工!
三、项目实践
1. 初始目录结构
D:\idea_workspace\springboot_test>tree /f
文件夹 PATH 列表
卷序列号为 0000-8AEC
D:.
└─springboot-rest
│ pom.xml
│ springboot-rest.iml
│
├─.idea
│ │ .name
│ │ compiler.xml
│ │ encodings.xml
│ │ misc.xml
│ │ modules.xml
│ │ vcs.xml
│ │ workspace.xml
│ │
│ └─copyright
│ profiles_settings.xml
│
└─src
├─main
│ ├─java
│ └─resources
└─test
└─java
2.修改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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yale</groupId>
<artifactId>springboot-rest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</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>
§
<relativePath/> 设定一个空值将始终从仓库中获取,不从本地路径获取。
右击项目,选择Maven,再选择Reimport,从远程仓库加载依赖与插件。
3.代码
i. 创建一个Application类,用来启动项目
package com.yale;
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);
}
}
ii. 创建一个StudentService类,用来提供服务
package com.yale;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("student")
public class StudentService {
@GetMapping
public Map<String,Object> sayHello(){
Map<String,Object> result = new HashMap<>();
result.put("message","hello, nice to meet you!");
return result;
}
}
4.测试
i. 启动项目
如何在IDEA中运行一个Maven项目?
1)选择顶部菜单栏Run,再选择Edit Configurations
2)在Configurations窗口左上方,点击“+”图标,选择Maven
3)然后在右侧信息栏,填写Name和Command line
Name:spring-boot:run
Command line:spring-boot:run
4)最后选择顶部菜单栏Run -- debug ‘spring-boot:run’,即可启动项目。
ii. 浏览器访问
+ 打开浏览器
+ 访问:http://localhost:8080/student
+ 页面显示:
-------------------------------
{"message":"hello, nice to meet you!"}
-------------------------------
从结果上可以知道,
服务中返回的Map结构数据,在页面显示时,已经转化为Json。
其实,返回一个Entity也会转化成json,不信你试试!