SpringBoot | 跟随官网的脚步 | 110: URL

2018-09-11  本文已影响0人  码农学禅

目标

这次前进一小步,对localhost:8080/ 能够响应Hello World。

要点

@RestController、@RequestMapping

代码

import org.springframework.boot.*;

import org.springframework.boot.autoconfigure.*;

import org.springframework.web.bind.annotation.*; //new in 110

@RestController //new in 110

@EnableAutoConfiguration

public class example {

    @RequestMapping("/")

    String home(){

    return "Hello World:/";

    }

    public static void main(String args[]) throws Exception{

        SpringApplication.run(example.class, args);

    }

}

所有新增加的语句后面都有注释//new in 110

说明

大多数文章里看到是@Controller,而这里是@RestController。两者的区别在于:@Controller返回的字符串是模板文件名,而@RestController返回的就是Response Body

参考

官网 | Developing Your First Spring Boot Application

上一篇下一篇

猜你喜欢

热点阅读