SpringBoot 2 : 网络配置

引入 Web 模块

1、pom.xml中添加支持web的模块:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

pom.xml文件中默认有两个模块:

spring-boot-starter:核心模块,包括自动配置支持、日志和YAML;

spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito。

2、编写controller内容


@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}

@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!

3、启动主程序,打开浏览器访问http://localhost:8080/hello,就可以看到效果了,有木有很简单!

原文地址:https://www.cnblogs.com/Plorde/p/13191355.html