详解Springboot快速搭建跨域API接口的步骤(idea社区版2023.1.4+apache-maven-3.9.3-bin)

 更新时间:2023年07月25日 16:25:18   作者:红目香薰  
这篇文章主要介绍了Springboot快速搭建跨域API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin),本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下

目标:启动程序后可访问接口。

启动中。

环境准备

idea版本:IntelliJ IDEA Community Edition 2023.1.4

Maven版本:apache-maven-3.9.3-bin

Maven镜像文件setting.xml配置

我这里用的是阿里云的镜像地址。

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- 存储位置,注意自己改到自己电脑合适的位置 -->
  <localRepository>D:\save\exe\AllLog\mavenjar</localRepository>
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
  </servers>
  <mirrors>
   	 <!-- 阿里云镜像 -->
		<mirror> 
		<id>alimaven</id> 
		<name>aliyun maven</name> 
		<url>http://maven.aliyun.com/nexus/content/repositories/central/</url> 
		<mirrorOf>central</mirrorOf> 
		</mirror>
		<!-- junit镜像地址 -->
		<mirror> 
		<id>junit</id> 
		<name>junit Address/</name> 
		<url>http://jcenter.bintray.com/</url> 
		<mirrorOf>central</mirrorOf> 
		</mirror>
        <mirror>  
            <id>alimaven</id>  
            <name>aliyun maven</name>  
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
            <mirrorOf>central</mirrorOf>  
        </mirror>
  </mirrors>
  <profiles>
  </profiles>
</settings>

1、创建项目

2、创建项目时的Maven选项

3、创建项目完毕效果

4、修改项目引用的Maven

这里选择我们自己的Maven,不用系统默认的。

选则Maven的setting.xml位置

5、刷新maven

6、添加springboot的pom配置

引入2.3.4的spring-boot

  <!-- 引入2.3.4的spring-boot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>

引入dependencies配置

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

7、再次刷新maven

确认引入完成。

8、在main上点击鼠标右键【new】->【Directory】

9、添加java包

10、编写启动文件Action.java

看好package路径啊【com.item】下的【Action.java】

package com.item;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Action {
    public static void main(String[] args) {
        //一定是被@SpringBootApplication标记的类
        SpringApplication.run(Action.class, args);
    }
}

11、编写接口类UsersController

package com.item.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@CrossOrigin
public class UsersController {
    @GetMapping("GetInfo")
    public Object GetInfo() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("state", true);
        map.put("msg", "成功");
        map.put("result", "有一个字符串");
        return map;
    }
}

12、启动Action.java文件

接口效果呈现

跨域效果呈现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
    <script>
        $.ajax({
            url:"http://localhost:8080/GetInfo",
            type:"get",
            dataType:"json",
            success:function(res){
                console.log(res);
            }
        });
    </script>
</body>
</html>

总结

到此,springboot的基本配置完成,这个是社区版本的,起始与企业版本的没啥大区别,都是一样处理的。希望能给刚入学的孩子们带来一些学习上的方便。

资源地址:https://dxz.jb51.net/202307/yuanma/springapijk_jb51.rar

到此这篇关于Springboot快速搭建跨域API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin)的文章就介绍到这了,更多相关Springboot搭建跨域API接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Spring AOP @AspectJ进阶说明

    基于Spring AOP @AspectJ进阶说明

    这篇文章主要介绍了基于Spring AOP @AspectJ进阶说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • shardingsphered 线程安全问题示例分析

    shardingsphered 线程安全问题示例分析

    这篇文章主要为大家介绍了shardingsphered 线程安全问题示例分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • 利用MyBatis实现条件查询的方法汇总

    利用MyBatis实现条件查询的方法汇总

    这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-08-08
  • Java使用flyway实现脚本自动化的方法详解

    Java使用flyway实现脚本自动化的方法详解

    Flyway是一个开源的数据库版本控制工具,主要用于管理数据库的版本和变更,它可以自动化地将数据库迁移到不同的版本,同时支持多种数据库类型,本文给大家介绍了如何使用flyway实现脚本自动化,需要的朋友可以参考下
    2023-10-10
  • Springboot ApplicationRunner的使用解读

    Springboot ApplicationRunner的使用解读

    这篇文章主要介绍了Springboot ApplicationRunner的使用解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Java中使用Filter过滤器的方法

    Java中使用Filter过滤器的方法

    Filter过滤器是javaWeb层面的,它跟Servlet类似,每次前端请求,首先进入的是过滤器,我们必须实现Filter接口,重写三个方法,才能使用Filter过滤器,需要的朋友可以参考下
    2021-06-06
  • SpringBoot项目不占用端口启动的方法

    SpringBoot项目不占用端口启动的方法

    这篇文章主要介绍了SpringBoot项目不占用端口启动的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Java的string类为什么是不可变的

    Java的string类为什么是不可变的

    这篇文章主要介绍了Java的string类为什么是不可变的,总结了三个答案,需要的朋友可以参考下
    2014-04-04
  • Idea安装Eslint插件提示:Plugin NativeScript was not installed的问题

    Idea安装Eslint插件提示:Plugin NativeScript was not installed的问题

    这篇文章主要介绍了Idea安装Eslint插件提示:Plugin NativeScript was not installed的问题,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代码

    SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代码

    这篇文章给大家介绍了SpringBoot使用classfinal-maven-plugin插件加密Jar包的实例,文中通过代码示例和图文讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-02-02

最新评论