SpringBoot 第三篇 Controller

RestController注解等同于Controller注解加ResponseBody注解

根据上一篇文章我们修改HelloController的文件如下:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String say( ) {

//        return myLong;
//        return content;
        return kaBiSouProperties.getName();
    }
}

这里就把RestController换成Controller和ResponseBody,运行项目并访问:

多个action访问,在RequestMapping里增加映射元素,修改后如下:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value={"/hello","hi"},method = RequestMethod.GET)
    public String say( ) {

//        return myLong;
//        return content;
        return kaBiSouProperties.getName();
    }
}

访问结果如下:

都可以成功访问

接着给整个设置映射,使其可以访问整个类,以及类里面的方法:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value={"/hello","/hi"},method = RequestMethod.GET)
    public String say( ) {

//        return myLong;
//        return content;
        return kaBiSouProperties.getName();
    }
}

重启系统访问:

修改请求方式:

我修改请求方式为:post, 用postman访问地址:

得到内容,说明是POST请求方式。

当不写请求方式的时候:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value={"/hello","/hi"})
    public String say( ) {

//        return myLong;
//        return content;
        return kaBiSouProperties.getName();
    }
}

访问得到如下:

但是不建议这样写,因为这样写不安全

@PathVariable 获取url的数据
@RequestParam 获取请求中的参数
@GetMapping 组合注解

修改HelloController如下:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value="/hello/{id}",method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id ) {

//        return myLong;
//        return content;
//        return kaBiSouProperties.getName();
        return "id:"+id;
    }
}

重启系统访问http://localhost:8081/Test/hello/23

RequestParam传参,修改后如下:
package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String hello(@RequestParam("id") Integer myId ) {

//        return myLong;
//        return content;
//        return kaBiSouProperties.getName();
        return "id:"+myId;
    }
}

重启系统访问:

package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String hello(@RequestParam(value="id",required = false,defaultValue = "0") Integer myId ) {

//        return myLong;
//        return content;
//        return kaBiSouProperties.getName();
        return "id:"+myId;
    }
}

重启系统访问:

可以看到,不传参id的时候,默认值为0;

@GetMapping组合注解:
package com.lvzhihao.demo;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController
@Controller
@ResponseBody
@RequestMapping("Test")
public class HelloController {
@Autowired
private KaBiSouProperties kaBiSouProperties;


// @Value("${myLong}")
// private String myLong;


// @Value("${content}")
// private String content;

//    @RequestMapping(value="/hello", method = RequestMethod.GET)
    @GetMapping(value ="/hello")
    public String hello(@RequestParam(value="id",required = false,defaultValue = "0") Integer myId ) {

//        return myLong;
//        return content;
//        return kaBiSouProperties.getName();
        return "id:"+myId;
    }
}

重启系统访问:

还有对应的几种组合注解:

参考来自慕课网

猜你喜欢

转载自blog.csdn.net/qq_38000902/article/details/82495397