欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Springboot快速搭建跨域API接口的步驟(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)

 更新時間:2023年07月25日 16:25:18   作者:紅目香薰  
這篇文章主要介紹了Springboot快速搭建跨域API接口(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin),本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下

目標:啟動程序后可訪問接口。

啟動中。

環(huá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)建項目

2、創(chuàng)建項目時的Maven選項

3、創(chuàng)建項目完畢效果

4、修改項目引用的Maven

這里選擇我們自己的Maven,不用系統(tǒng)默認的。

選則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文件

接口效果呈現(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的基本配置完成,這個是社區(qū)版本的,起始與企業(yè)版本的沒啥大區(qū)別,都是一樣處理的。希望能給剛?cè)雽W的孩子們帶來一些學習上的方便。

資源地址: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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論