SpringBoot的启动文件如下:
package net.xdclass.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class XdclassApplication {
public static void main(String[] args) {
SpringApplication.run(XdclassApplication.class, args);
}
}
其中@SpringBootApplication该注解包含了三个注解如下:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
其本质可以理解为:
@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
其余注解:
@RestController = @Controller+@ResponseBody
测试代码:
package net.xdclass.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@RequestMapping("/")
public String home(){
return "Hello World!-NEW";
}
@RequestMapping("/test")
public Map<String, String> testMap(){
Map<String, String> map = new HashMap<String, String>();
map.put("name", "xdclass");
return map;
}
}
页面显示:
这样就不用再每个方法上面写@ResponseBody