詳解Springboot快速搭建跨域API接口的步驟(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)
目標(biāo):啟動(dòng)程序后可訪問接口。
啟動(dòng)中。
環(huán)境準(zhǔn)備
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、創(chuàng)建項(xiàng)目
2、創(chuàng)建項(xiàng)目時(shí)的Maven選項(xiàng)
3、創(chuàng)建項(xiàng)目完畢效果
4、修改項(xiàng)目引用的Maven
這里選擇我們自己的Maven,不用系統(tǒng)默認(rèn)的。
選則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
確認(rèn)引入完成。
8、在main上點(diǎn)擊鼠標(biāo)右鍵【new】->【Directory】
9、添加java包
10、編寫啟動(dòng)文件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標(biāo)記的類 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", "有一個(gè)字符串"); return map; } }
12、啟動(dòng)Action.java文件
接口效果呈現(xiàn)
跨域效果呈現(xiàn)
<!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>
總結(jié)
到此,springboot的基本配置完成,這個(gè)是社區(qū)版本的,起始與企業(yè)版本的沒啥大區(qū)別,都是一樣處理的。希望能給剛?cè)雽W(xué)的孩子們帶來一些學(xué)習(xí)上的方便。
資源地址:https://dxz.jb51.net/202307/yuanma/springapijk_jb51.rar
到此這篇關(guān)于Springboot快速搭建跨域API接口(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)的文章就介紹到這了,更多相關(guān)Springboot搭建跨域API接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot3整合SpringDoc OpenAPI生成接口文檔的詳細(xì)過程
- 關(guān)于springboot忽略接口,參數(shù)注解的使用ApiIgnore
- Springboot+Redis實(shí)現(xiàn)API接口防刷限流的項(xiàng)目實(shí)踐
- SpringBoot?快速實(shí)現(xiàn)?api?接口加解密功能
- SpringBoot整合Sa-Token實(shí)現(xiàn)?API?接口簽名安全校驗(yàn)功能
- SpringBoot如何根據(jù)目錄結(jié)構(gòu)生成API接口前綴
- SpringBoot可視化接口開發(fā)工具magic-api的簡單使用教程
- SpringBoot實(shí)現(xiàn)API接口的完整代碼
- springboot接入方式對接股票數(shù)據(jù)源API接口的操作方法
相關(guān)文章
Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場景
本文將結(jié)合實(shí)例代碼,介紹Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場景,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07springboot升級到j(luò)dk21最新教程(2023年)
你還在使用jdk8?快來看看最新出爐的SpringBoot+jdk21如何使用,下面這篇文章主要給大家介紹了關(guān)于springboot升級到j(luò)dk21的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Java中轉(zhuǎn)義字符反斜杠\的代替方法及repalceAll內(nèi)涵解析
這篇文章主要介紹了Java中轉(zhuǎn)義字符反斜杠\的代替方法及repalceAll內(nèi)涵解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08java 使用簡單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大
本篇文章介紹了,在java中使用簡單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大。需要的朋友參考下2013-05-05idea 隱藏target,iml等不需要展示的文件(推薦)
這篇文章主要介紹了idea 隱藏target,iml等不需要展示的文件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11SpringBoot應(yīng)用程序轉(zhuǎn)換成WAR文件詳解
其實(shí)一般使用SpringBoot使用打成jar包比較省事的,但也有很多童鞋是習(xí)慣使用WAR包的,下面這篇文章主要給大家介紹了關(guān)于SpringBoot轉(zhuǎn)換WAR的相關(guān)資料,需要的朋友可以參考下2022-11-11Spring Boot 實(shí)現(xiàn)配置文件加解密原理
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Mybatis實(shí)現(xiàn)自定義的typehandler三步曲
這篇文章主要介紹了Mybatis實(shí)現(xiàn)自定義的typehandler三步曲的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07