1、pom.xml
<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> </dependencies>
2、项目层次
3、启动文件【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) { SpringApplication.run(Action.class,args);//一定是被@SpringBootApplication标记的类 } }
4、controller文件
其中@RestController = @Controller + @ResponseBody;
package com.item.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; 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; } }
5、启动测试(数据是跨域的)
访问路径:【http://127.0.0.1:8080/GetInfo】