1.@Controller必须配合模版来使用
在resources目录的templates目录下添加一个hello.html文件
代码:
@Controller
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
@Controller的作用就是当访问/hello这个路径时,渲染以hello为名字的模板
2.@RestController是@ResponseBody和@Controller的组合注解。
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
@RestController的返回值是一个字符串
3.@RequestMapping 可以根据不同的请求路径执行不同的功能,并且可以加在类上也可以加在方法上
//该方法的作用地址为localhost:8080/hello
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
//该方法的作用地址为localhost:8080/hello/sayHello
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value="/sayHello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}