Spring boot中Controller的使用

您所在的位置:网站首页 springboot中的controller的作用 Spring boot中Controller的使用

Spring boot中Controller的使用

2023-08-23 15:32| 来源: 网络整理| 查看: 265

一、请求及路径映射部分注解介绍 注解名称 描述 @Controller 处理http请求 @RestController Spring4之后新加的注解,原来返回json,需要@ResponseBody配合@Controller @RequestMapping 配置url映射

 

 

 

 

 

1、@Controller的使用(了解)

相当于serverlet的jsp,前后端不分离的开发,就模板而言很好性能,所以提倡前后端分离式的开发,这里我就不啰嗦了。

接着再来演示下,这个注解的使用,必须配合模板来使用,首先在pom文件中添加如下依赖:

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

说明:这里的spring boot版本必须为1.4.1,否则使用模板时,不显示页面,报错404

org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE

在src/main/resources/templates/下,

创建一个名为index的html文件,写入内容如下:

hello,Spring boot!

编写Controller,示例如下:

package com.rongrong.springboot.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * @author rongrong * @version 1.0 * @description: * @date 2019/12/28 21:32 */ @Controller public class HtmlController { @RequestMapping(value = "/say",method = RequestMethod.GET) public String index(){ return "index"; } }

启动项目,访问页面:http://localhost:8888/say,显示结果如下:

 

 

 2、@RestController的使用

@RestController在实际开发中应用广泛,与@controller相比,不依赖于模板,前后端分离式的开发,开发效率也很高。

3、@RequestMapping的使用

该注解既可以在类上方使用,又可以方法上使用,在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问,如:localhost:8888/spring/hellow

示例代码如下:

package com.rongrong.springboot.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author rongrong * @version 1.0 * @description: * @date 2019/12/26 20:34 */ @RestController /** * 在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问 * 如:localhost:8888/spring/hellow */ @RequestMapping("/spring") public class HellowController { @Autowired Person person; //在方法 上方使用 @RequestMapping(value = "/hellow",method = RequestMethod.GET) public String say(){ return person.getGName(); } }

访问接口地址:http://localhost:8888/spring/hellow,具体效果如下:

 

 

 

二、请求参数及组合注解介绍 注解名称 描述 @PathVariable 获取url中的数据 @RequestParam  获取请求参数的值 @GetMapping 组合注解

 

 

 

  1、@PathVariable的使用

一般在restful风格接口使用居多,形式为:/路径/{变量}

具体示例如下:

//Restful风格接口 @RequestMapping(value = "/getID/{id}", method = RequestMethod.GET) public String getID(@PathVariable("id") Integer id) { return "Id:"+id; }

访问示例:http://localhost:8888/spring/getID/1,效果如下:

 

 

 2、@RequestParam使用

 为传统风格形式使用,具体示例如下:

/*** * 传统方式接口 * @RequestParam(value = "id",required = false,defaultValue = "0") * value为传入字段名,required为true时必须传参,defaultValue为传入参数默认值 * @param id * @return */ @RequestMapping(value = "/getMyId", method = RequestMethod.GET) public String getMyId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) { return "getMyId: \t"+id; }

访问示例:http://localhost:8888/spring/getMyId?id=1,效果如下:

 

 

 3、组合注解及多个路径可以访问同一个接口

@GetMapping可以理解为@RequestMapping,具体示例如下:

/** * 组合注解及多个路径可以访问同一个接口 * @param id * @return */ @GetMapping({"/getUserId","/sayhi"}) //@RequestMapping(value = "/getMyId", method = RequestMethod.GET) public String getUserId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) { return "getMyId: \t"+id; }

访问示例:

http://localhost:8888/spring/getUserId?id=1,

http://localhost:8888/spring/sayhi?id=1

效果如下:

 

 

 

优秀不够,你是否无可替代

软件测试交流QQ群:721256703,期待你的加入!!

欢迎关注我的微信公众号:软件测试君



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3