135-1821-9792

springboot-controller的使用详解

Controller的使用

创新互联建站主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、营销型网站建设、网站程序开发、HTML5响应式网站建设公司成都手机网站制作、微商城、网站托管及网站建设维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都格栅板行业客户提供了网站开发服务。

一、

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

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

使用spring官方的一个模板:


  org.springframework.boot
  spring-boot-starter-thymeleaf

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

hello Spring Boot!

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

结果:

springboot-controller的使用详解

方式二:也可以把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();
  }
}

结果:

springboot-controller的使用详解

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

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

结果:

springboot-controller的使用详解

注解简写:@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();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


本文标题:springboot-controller的使用详解
标题URL:http://kswsj.com/article/jphcgd.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号