企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、概述 这里,通过一个完整的例子,说明springboot的典型的完整技术堆栈; ## 二、引入model ``` public class Staff { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 在RayController中增加: ``` @RequestMapping("/staff") public Staff getString() { Staff staff =new Staff(); staff.setId(1); staff.setName("张三"); return staff; } ``` 继续访问:http://localhost:8080/staff得到效果: ![](https://img.kancloud.cn/5b/70/5b70db5001252d28a326e4b1dd9ceac4_1366x736.png) > 只需要Controller类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回 ## 三、配置文件 例如: ``` ray.config.appMaxUser=9 ray.config.appTitle=\u65B0\u4E00\u4EE3\u5E73\u53F0 ray.config.enabled=true ``` 配置在 application.properties 中,可以将所有的配置信息映射为一个独立的javabean; ``` @ConfigurationProperties(prefix = "ray.config") public class GloabalProperties { private long appMaxUser; private String appTitle; private Boolean enabled; //getter/setter ``` 在@SpringBootApplication中定义: ``` @SpringBootApplication @EnableConfigurationProperties({ GloabalProperties.class }) public class RayframeworkApplication { public static void main(String[] args) { SpringApplication.run(RayframeworkApplication.class, args); } } ``` 然后,在各种地方可以使用了; ``` @Autowired private AccessDao accessDao; @Autowired private GloabalProperties gloabalProperties; @RequestMapping("/sampleDbObject") public Access sampleDbObject() { Access access = new Access(); accessDao.save(access); access.setBROWSER_INFO(gloabalProperties.getAppTitle()); return access; } ```