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

相关推荐
whinc5 小时前
Rust技术周刊 2026年第17周
后端·rust
whinc5 小时前
Rust技术周刊 2026年第18周
后端·rust
whinc5 小时前
Rust技术周刊 2026年第16周
后端·rust
jieyucx5 小时前
Go语言深度解剖:Map扩容机制全解析(增量扩容+等量扩容+渐进式迁移)
开发语言·后端·golang·map·扩容策略
王码码20355 小时前
Go语言的内存管理:原理与实战
后端·golang·go·接口
Lee川6 小时前
打字机是怎么炼成的:Chat 流式输出深度解析
前端·后端·面试
Lee川6 小时前
Token 无感刷新与 Logout:前端安全会话管理实战
前端·后端·react.js
舒一笑7 小时前
零后端、零数据库——我做了一个让 10000+ 人成功告白的开源工具
后端·产品·设计师
Java技术小馆8 小时前
如何零成本将各种 AI 编程工具接入免费大模型?
后端
Tutankaaa8 小时前
从10队到50队:知识竞赛软件的高并发场景如何设计?
java·经验分享·后端·spring