当前位置: 首页 > news >正文

Spring Boot Controller

刚入门小白,详细请看这篇SpringBoot各种Controller写法_springboot controller-CSDN博客


Spring Boot 提供了@Controller和@RestController两种注解。

@Controller

返回一个string,其内容就是指向的html文件名称。


@Controller
public class HelloController {@RequestMapping("/hello")public String index(ModelMap map) {map.addAttribute("name", "ge");return "hello";}
}

@RestController

将返回对象数据转换为JSON格式。

@RestController
public class HelloController {@RequestMapping("/user")public User getUser() {User user = new User();user.setUsername("ge");user.setPassword("kaimen");return user;}
}

@RequestMapping

负责URL路由映射。

如果添加在Controller类上,则该Controller中所有路由映射都会加上该映射规则;

如果添加在方法上,则只对当前方法生效。

属性:value, method, consumes, produces, params, headers 等

@RequestMapping(value = "/getData", method = RequestMethod.GET)public String getData() {return "hello";
}

Method 匹配也可以用 @GetMapping, @PostMapping 等代替。

http://www.lryc.cn/news/192350.html

相关文章:

  • 在网络安全、爬虫和HTTP协议中的重要性和应用
  • Web测试框架SeleniumBase
  • jvm打破砂锅问到底- 为什么要标记或记录跨代引用
  • 小程序长期订阅
  • Studio One6.5中文版本版下载及功能介绍
  • 07-Zookeeper分布式一致性协议ZAB源码剖析
  • 云原生安全应用场景有哪些?
  • Step 1 搭建一个简单的渲染框架
  • Excel 插入和提取超链接
  • 基础架构开发-操作系统、编译器、云原生、嵌入式、ic
  • C++-Mongoose(3)-http-server-https-restful
  • git多分支、git远程仓库、ssh方式连接远程仓库、协同开发(避免冲突)、解决协同冲突(多人在同一分支开发、 合并分支)
  • ChatGPT或将引发现代知识体系转变
  • 【爬虫实战】用pyhon爬百度故事会专栏
  • 焦炭反应性及反应后强度试验方法
  • 链表(3):双链表
  • 【TES720D】基于复旦微的FMQL20S400全国产化ARM核心模块
  • Python 列表切片陷阱:引用、复制与深复制
  • macbook电脑删除app怎么才能彻底清理?
  • 【数据结构】二叉树--链式结构的实现 (遍历)
  • reids基础数据结构
  • gitlab 维护
  • ABB机器人RWS连接方法
  • Spring Boot的循环依赖问题
  • postgresql|数据库|恢复备份的时候报错:pg_restore: implied data-only restore的处理方案
  • Elasticsearch:使用 Langchain 和 OpenAI 进行问答
  • 安全巡检管理系统—隐患排查治理
  • 第9期ThreadX视频教程:自制个微秒分辨率任务调度实现方案(2023-10-11)
  • C++ 11 lamdba表达式详解
  • Linux运行环境搭建系列-Zookeeper安装