spring-boot 搭建web项目
一、创建项目或模块
二、导入依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spirngbootweb</artifactId> <version>1.0-SNAPSHOT</version> <!--导入spring-boot 启动父类--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <!--导入web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.1.RELEASE</version> </dependency> </dependencies> </project>
三、配置application.yml
在resource目录下创建application.yml,配置如下信息
server: port: 8080 spring: application: name: spirngbootweb
四、创建启动类
启动类位置要在项目最上层包下
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ManageApplication {
public static void main(String[] args) {
SpringApplication.run(ManageApplication.class);
}
}
五、创建controller
可以将对象自动转换为json字符串
package com.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @Controller public class DemoCotrller1 { @RequestMapping("list") @ResponseBody public Map findList() { Map map=new HashMap(); map.put("nema","张三"); map.put("age","15"); map.put("job","it"); return map; } }
六、测试
路径:http://localhost:8080/list
效果: