idea springboot学习(第二天)

第一天学习,总结了idea springboot的编辑器安装下载及基本项目搭建,并梳理了简单的错误异常!

第二天计划springboot语法学习!

注解:

第一个:@RestController

这个注解是Spring4之后新加入的注解,原来返回JSON需要@ResponseBody和@Controller配合

即@RestController是@ResponseBody和@Controller的组合注解

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return  "hello,this is a spring demo";
    }
}
运行返回结果是:hello,this is a spring demo

   这个效果同下面的效果一致!

   

@Controller

   @ResponseBody

public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return  "hello,this is a spring demo";
    }
}
第二个:@Controller 处理http请求

     @Controller

     //@ResponseBody

     public class HelloController{

          @RequestMapping(value="/hello",method=RequestMethod.GET)

           Public  String hello(){

              return "hello,this is a spring demo"

    如果直接隐形SpringBoot项目,输入请求,会提示Whitelabel Error Page,出现这种情况的原因是没有实用模板。

     @Controller用来响应页面,@Controller必须配合模板来使用。

      Spring-boot支持多种模板:FreeMarker   Groovy Thymeleaf velocity 

第三个:@RequestMapping配置url映射

              (1)仅作用在处理器方法上

                        

   public class HelloController{

          @RequestMapping(value="/hello",method=RequestMethod.GET)

           Public  String hello(){

              return "hello,this is a spring demo"}

               (2)仅作用在类的级别上

             @RequestMapping(value="/hello",method=RequestMethod.GET)

public class HelloController{

          @RequestMapping(method=RequestMethod.GET)

           Public  String hello(){

              return "hello,this is a spring demo"}

         事实证明仅仅作用在方法上和仅仅作用在类上,效果是一样的

                   (3)同时作用在类级别和处理器方法上

                          

 @RequestMapping("/sayhello")

public class HelloController{

          @RequestMapping(value="/hello",method=RequestMethod.GET)

           Public  String hello(){

              return "hello,this is a spring demo"}


            @RequestMapping(value="/hi",method=RequestMethod.GET)

   Public  String hello(){

              return "hello,this is a spring demo hi"}

   这样的话,请求的url=localhsot:8080/sayhello/hello   和  url=localhsot:8080/sayhello/hi

猜你喜欢

转载自blog.csdn.net/perry1230/article/details/80181381