从0开始使用IDEA搭建java REST api架构

前言

使用IntelliJ IDEA Community Edition 2022.1不知道这么创建java的REST API架构~不知道咋下手,那你看这篇文章就对了

开始

因为IDEA是社区版的,使用没有spring boot选项,我们要自己加一下

1.设置(seting) - 插件(plug)

2.搜索spring boot

安装上面这三个(或者第二个即可),然后重新打开IDEA。

新建项目

 

 

 

 

 

 项目新建完成!

这是入口文件

来到设置

 

 要等待一段时间

 安装好它会自己运行

 移控制窗口过去,可以看见默认是8080端口

 我们用浏览器访问一下

 先结束当前运行(IDEA会有问题,8080端口会一直被占用,我们要从任务管理器结束),我们创建一个路由,跟把端口改一下

 

这样才算是完整的结束了,不然你运行会一直提示你8080端口被占用导致你运行失败

修改端口号

server.port=8081

 新建一个HelloController.java文件,然后编写一个路由

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public Map<String, String> hello(@RequestParam("data") String data) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.put("code", "200");
            map.put("data", "");
            map.put("msg", "成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("data", "");
            map.put("msg", "失败");
            return map;
        }
    }
}

运行项目

 

 浏览器访问

 完成!

总结

IDEA - 装spring boot 插件 - 创建spring boot 项目 - 新建类、路由 并且返回json对象,跟接受接口传递的数据

猜你喜欢

转载自blog.csdn.net/echo_Ae/article/details/124364780