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

相关推荐
勇哥java实战分享3 小时前
程序员的明天:AI 时代下的行业观察与个人思考
后端
掘金码甲哥5 小时前
超性感的轻量级openclaw平替,我来给你打call
后端
用户8356290780518 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
啊哈灵机一动8 小时前
使用golang搭建一个nes 模拟器
后端
间彧9 小时前
SpringBoot + ShardingSphere 读写分离实战指南
后端
砍材农夫9 小时前
订单超时
后端
树獭叔叔10 小时前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
得鹿10 小时前
MySQL基础架构与存储引擎、索引、事务、锁、日志
后端
程序员飞哥10 小时前
Block科技公司裁员四千人,竟然是因为 AI ?
人工智能·后端·程序员
JavaEdge在掘金11 小时前
Claude Code 直连 Ollama / LM Studio:本地、云端开源模型都能跑
后端