SpringMVC Restful风格

在controller的类上添加@RestController注解

java 复制代码
package com.xiaoya.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class TestController {
    @RequestMapping("/hello/{name}")
    @ResponseBody
    public String hello(@PathVariable String name){
        return "hello "+name;
    }
}

在方法的参数前面标上@PathVariable注解,然后路径中使用{name}就可以解析变量

启动tomcat,访问http://localhost:8080/springmvc1_war_exploded/hello/2

相关推荐
从零开始的代码生活_2 分钟前
C++ list 原理与实践:双向链表、迭代器与简化实现
开发语言·c++·后端·学习·算法·链表·list
初学AI的小高4 分钟前
FastAPI 工程实践:LLM 应用的 API 设计之道
后端·fastapi
袋鱼不重34 分钟前
乐观锁与悲观锁:原理、选型与前后端实践
前端·后端
用户83562907805140 分钟前
使用 Python 对 PDF 文档进行数字签名的方法
后端·python
苏三说技术41 分钟前
Skywalking已全面拥抱AI!
后端
神奇小汤圆1 小时前
Maven 4 要来了:15 年后,Java 构建工具迎来“彻底重构”
后端
面试鸭1 小时前
面试官:“知识库搜到多少资料,你就全喂给 AI?”我:“信息越多越准。”她亮出错答案:“那它怎么被废话带跑了?”
后端·面试·求职
米花米唐1 小时前
Spring AI 2.0 会话记忆开发实践
后端
从零开始的代码生活_2 小时前
C++ stack、queue 与 priority_queue:容器适配器原理与实战
开发语言·c++·后端·学习·算法
爱勇宝2 小时前
《道德经》第6章:别把团队的创造力用到枯竭
前端·后端