spring controller springboot-controller的使用详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring controller springboot-controller的使用详解

JS_HCX   2021-03-29 我要评论
想了解springboot-controller的使用详解的相关内容吗,JS_HCX在本文为您仔细讲解spring controller的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot,controller,springbootcontroller,下面大家一起来学习吧。

Controller的使用

一、

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

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

使用spring官方的一个模板:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在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
  private 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();
  }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们