1、http的各个请求,各个注解使用
1、一个注解相当于三个注解@SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration
+@ComponentScan
2、@RestController返回json格式,就不需要每个方法都写@ResponseBody
@RestController=@Controller+@ResponseBody
3、@RestController 和@RequestMapping 是springMVC的注解,不是springboot特有的
main方法入口:
package net.xdclass.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication //一个注解顶下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class XdclassApplication {
public static void main(String[] args) {
SpringApplication.run(XdclassApplication.class, args);
}
}
测试接口:
package net.xdclass.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.*;
//@RestController返回json格式
//就不需要每个方法都写@ResponseBody
//@RestController=@Controller+@ResponseBody
@RestController
//自定义控制器访问名称
@RequestMapping("/sample")
public class SampleController {
@GetMapping("/index")//GET请求
String home() {
return "Hello World!";
}
//post请求,添加映射路径
@PostMapping("/test")
//@ResponseBody
public Map<String,String> testMap(){
Map<String,String> map = new HashMap<>();
map.put("name", "xdclass");
map.put("age", "25");
return map;
}
}