SpringBoot 第三篇 Controller

25 篇文章 0 订阅
10 篇文章 0 订阅

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;
    }
}

重启系统访问:

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

参考来自慕课网

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风而行无疾而歌

看官,觉得不错,奖励一下呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值