• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot-controller的使用详解

springboot 搞代码 4年前 (2022-01-05) 25次浏览 已收录 0个评论

本篇文章主要介绍了springboot-controller的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Controller的使用

一、

  • @Controller:处理http请求
  • @RestController:Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
  • @RequestMapping:配置url映射

1.对于控制器层,如果只使用@Controller注解,会报500,即controller必须配合一个模板来使用:

使用spring官方的一个模板:

  org.springframework.bootspring-boot-starter-thymeleaf

在resources下面的templates文件夹下建立index.html:

 <h1>hello Spring Boot!</h1>

HelloController:

 @Controller @ResponseBody public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ //    return girlProperties.getCupSize(); return "index"; } } 

@RestController相当于@Controller和@ResponseBody组合使用

如果程序需要通过hello和hi都能访问到,只需在@RequestMapping的value中添加如下:

 @RestController public class HelloController { @Autowired priva<p style="color:transparent">来源gao!%daima.com搞$代*!码网</p>te GirlProperties girlProperties; @RequestMapping(value = {"/hello", "/hi"},method = RequestMethod.GET) public String say(){ return girlProperties.getCupSize(); } } 

二、

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

@PathVariable:

方式一:

 @RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET) public String say(@PathVariable("id") Integer id){ return "id:"+id; //    return girlProperties.getCupSize(); } } 

结果:

方式二:也可以把id写在前面:

 @RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/{id}/say"},method = RequestMethod.GET) public String say(@PathVariable("id") Integer id){ return "id:"+id; //    return girlProperties.getCupSize(); } } 

结果:

方式三:使用传统方式访问:

 @RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/say",method = RequestMethod.GET) public String say(@RequestParam("id") Integer myId){ return "id:"+myId; //方法参数中的Integer id这个id不需要与前面对应 //    return girlProperties.getCupSize(); } } 

结果:

注解简写:@RequestMapping(value = “/say”,method = RequestMethod.GET)等价于:@GetMapping(value = “/say”)

 @RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; //  @RequestMapping(value = "/say",method = RequestMethod.GET) //@GetMapping(value = "/say")//等价于上面的 @PostMapping(value = "/say") public String say(@RequestParam("id") Integer myId){ return "id:"+myId; //方法参数中的Integer id这个id不需要与前面对应 //    return girlProperties.getCupSize(); } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是springboot-controller的使用详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot-controller的使用详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址