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

相关推荐
Python私教6 分钟前
Codex 自动写 Commit 够安全吗?一套证据化 Git 提交流程
人工智能·git·后端
Python私教10 分钟前
AI Agent 上生产要不要开写权限?我把执行链拆成 4 道闸门
人工智能·后端·python
lun22 分钟前
Claude Code 多 Agent 实现:subAgent & Agent Teams
后端
SomeB1oody2 小时前
【RustyML入门】2.9. MeanShift
开发语言·后端·机器学习·rust·教程
2401_894915533 小时前
GEO 源码部署如何实现精准地域分发?核心配置参数深度讲解
java·运维·服务器·后端·开源
IT_陈寒3 小时前
为什么我的Java Stream流操作会吃掉内存?
前端·人工智能·后端
Java技术小馆3 小时前
LangChain 概述与生态
后端
用户250694921614 小时前
优雅的数据隔离:PostgreSQL 行级安全(RLS)
后端
苍何5 小时前
用 WorkBuddy / Codex + Obsidian 搭建自生长的个人知识库实战
后端