首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >使用Spring Boot开发一个属于自己的web Api接口返回JSON数据

使用Spring Boot开发一个属于自己的web Api接口返回JSON数据

作者头像
大数据小禅
发布2021-08-16 18:08:47
发布2021-08-16 18:08:47
2.7K00
代码可运行
举报
文章被收录于专栏:YO大数据YO大数据
运行总次数:0
代码可运行

Spring Boot环境搭建


相关软件以及环境:

  • JDK1.8+
  • Maven3.5+
  • IDEA编辑器
  • PostMan接口测试神器

Spring Boot的搭建有两种较快的方式:

  1. Maven依赖创建
  2. 官网快捷在线创建https://start.spring.io/ (推荐)

第一种方式使用IDEA创建一个Maven工程即可,需要导入的依赖如下:

代码语言:javascript
代码运行次数:0
运行
复制
<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.2.2.RELEASEversion>
		<relativePath/> 
parent>


<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
  
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintagegroupId>
					<artifactId>junit-vintage-engineartifactId>
				exclusion>
			exclusions>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

第二种在线创建方式,访问https://start.spring.io/ 之后会生成一个ZIP的包,解压使用IDEA导入即可

成功导入后可以看到生成的目录结构,以及主类(DemoApplication.class),这个类的作用是扫描所有的字类,并启动我们的Sprint Boot 应用程序:

代码语言:javascript
代码运行次数:0
运行
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//这个类要放在最外层才可以扫描子包的东西
		SpringApplication.run(DemoApplication.class, args);
	}

}
创建第一个Web接口,返回JSON数据

我们在搭建好的Maven项目里面新建一个包,创建java文件 相关参数:

  • @RestController 作用:用于标记这个类是一个控制器,返回JSON数据的时候使用,如果使用这个注解,则接口返回数据会被序列化为JSON @RequestMapping 作用:路由映射,用于类上做1级路径;用于某个方法上做子路径

代码如下

代码语言:javascript
代码运行次数:0
运行
复制
package net.test.demo.controller;

import net.xdclass.demo.config.WXConfig;
import net.xdclass.demo.utils.JsonData;

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("api/v1/test")   
public class TestController {
    @GetMapping("testJson")
    public Object testJson(){
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"第一个接口测试");
        map.put(2,"test");
        map.put(3,"test");

        return map;
    }
}

运行DemoApplication.class启动应用程序,成功启动会显示如下内容:

Spring Boot的默认端口访问为8080,当然这个也可也在相关的配置文件进行修改,访问测试可以使用浏览器输入localhost:8080/api/v1/test/testJson,在日常工作中,JSON格式的数据也是后端跟前端交互使用最多的一种数据格式,也可也使用接口测试软件PostMan,测试结果如下,可以成功返回Json数据

到这里,一个基于Spring Boot搭建的后端Web接口搭建完成。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/04/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Boot环境搭建
    • 创建第一个Web接口,返回JSON数据
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档